   clear;
    global measureIni  ts1Ini  ts2Ini;
//Constant
measureIni=['20';'20000';'8';'2';'0.25'];
ts1Ini=['3.76';'0.198e-3';'3.382'];
ts2Ini=['7.54768e-4';'1.466e-3';'0.536e-3';'0.23'];

function caculateAgain() 
    global measureIni  ts1Ini  ts2Ini;
     tic()
    txt = ['起始頻率(Hz)';'結束(最大)頻率(Hz)';'octave(1/x)    ';'輸入電壓(V)';'量測距離(M)'];
    measure = x_mdialog('輸入測量條件',txt,measureIni)
    measureIni=measure;
    fstart = evstr(measure(1));
    fend = evstr(measure(2));
    octave = evstr(measure(3));
    mText=msprintf('起始頻率:%10.2e (Hz)結束(最大)頻率: %10.2e(Hz)',fstart,fend)

    finaloct = round(fend/fstart/octave);
    // Input
    eg=evstr(measure(4));
    rr=evstr(measure(5));
    R20 = logspace(log10(fstart),log10(fend),finaloct);//logspace

    rho = 1.25; // air density
    c = 343; //sound speed
    // electrical parameters
    txt2 = ['Re(ohm)';'Le(H)';'BL(n/a)    '];
    ts1 = x_mdialog('輸入TS參數',txt2,ts1Ini)
    ts1Ini=ts1;
    Revc = evstr(ts1(1)); // Re
    Levc = evstr(ts1(2)); // Le
    BL = evstr(ts1(3)); //BL


    // mechanical parameters
    txt3 = ['Sd(M^2)';'Mms(kg)';'Cms(M)    ';'Rms(Kg/s)'];
    ts2 = x_mdialog('輸入TS參數',txt3,ts2Ini)
    ts2Ini=ts2;
    A = evstr(ts2(1)); // Sd
    Mm = evstr(ts2(2)); //Mms
    Cm = evstr(ts2(3)); //Cms
    Rm =evstr(ts2(4));//Rms

    F0 = 1/2/%pi*sqrt(1/Mm/Cm);
    f=R20;
    w=2*%pi*f;k=w/c;s=%i.*w;

    for i=1:length(f)
        s(i)=2*%pi*f(i);
        V=[eg;0];
        R=[Revc+s(i)*Levc,BL;-BL,Rm+1/s(i)/Cm+s(i)*Mm];
        I(:,i)=inv(R)*V;
    end
    Imp=eg./I(1,:);
    Disp=abs(I(2,:)./s)*1000;
    SP=rho/(2*%pi).*s*A.*I(2,:).*exp(-%i.*k*rr)/rr;
    SPL=20*log10(abs(SP)./20e-6);
    toc()
    clf();

    //figure("Figure_name","Result")
    subplot(2,2,1);
    plot2d("ln",f,SPL,,"181");
    xtitle('SPL','Hz','dB');
    subplot(2,2,2);
    plot2d("ln",f,Imp);
    xtitle('IMP','Hz','Ohm');
    subplot(2,2,3);
    plot2d("ln",f,Disp);
    xtitle('Disp','Hz','mm');

    subplot(2,2,4);
    f=gcf();
    xstring(0.0,0.1,mText);
    // create a figure
   h=uicontrol(f,'style','pushbutton', ...
   'position', [300 10 150 30],...
   'string',"重新計算",...
   'callback','caculateAgain');
endfunction 
caculateAgain();

