试图在 Scilab 中做一个高斯钟

Trying to do a gaussian bell in Scilab

我正在尝试使用从矩阵获得的数据做一个高斯钟,但每次我尝试 运行 程序时,我都会收到此消息:

"Error: syntax error, unexpected identifier, expecting end"

得到gaussina bell的数据是一个矩阵,每n个位移的最后一个点,也就是一个粒子的最后位置。我想知道是否有更简单的方法在 scilab 中获得高斯钟,因为我还必须使用相同的数据对直方图进行拟合。

    function bla7()


t=4000
n=1000
l=0.067
p=%pi*2
w1=zeros(t,1);
w2=zeros(t,1);
for I=1:t
    a=(grand(n,1,"unf",0,p));
    x=l*cos(a)
    y=l*sin(a)
    z1=zeros(n,1);
    z2=zeros(n,1);
    for i=2:n
        z1(i)=z1(i-1)+x(i);
        z2(i)=z2(i-1)+y(i);
    end
    w1(I)=z1($)
    w2(I)=z2($)
end

n=10000
w10=zeros(t,1);
w20=zeros(t,1);
for I=1:t
    a=(grand(n,1,"unf",0,p));
    x=l*cos(a)
    y=l*sin(a)
    z1=zeros(n,1);
    z2=zeros(n,1);
    for i=2:n
        z1(i)=z1(i-1)+x(i);
        z2(i)=z2(i-1)+y(i);
    end
    w10(I)=z1($)
    w20(I)=z2($)
end

n=100
w100=zeros(t,1);
w200=zeros(t,1);
for I=1:t
    a=(grand(n,1,"unf",0,p));
    x=l*cos(a)
    y=l*sin(a)
    z1=zeros(n,1);
    z2=zeros(n,1);
    for i=2:n
        z1(i)=z1(i-1)+x(i);
        z2(i)=z2(i-1)+y(i);
    end
    w100(I)=z1($)
    w200(I)=z2($)
end

k=70
v=12/k
    c1=zeros(k,1)
    for r=1:t
        c=w1(r)
        m=-6+v
        n=-6
        for g=1:k
        if (c<m & c>=n) then
            c1(g)=c1(g)+1
            m=m+v
            n=n+v
        else
            m=m+v
            n=n+v
        end
        end
    end
c2=zeros(k,1)
c2(1)=-6+(6/k)
for b=2:k
    c2(b)=c2(b-1)+v
end
y = stdev(w1)

normal1=zeros(k,1)
normal2=zeros(k,1)
bb=-6
bc=-6+v

for wa=1:k
    bd=(bb+bc)/2
    gauss1=(1/(y*sqrt(2*%pi)))exp(-0.5(bb/y)^2)
    gauss2=(1/(y*sqrt(2*%pi)))exp(-0.5(bc/y)^2)
    gauss3=(1/(y*sqrt(2*%pi)))exp(-0.5(bd/y)^2)
    gauss4=((bc-bb)/6)*(gauss1+gauss2+4*gauss3)
    bb=bb+v
    bc=bc+v
    normal2(wa,1)=gauss4
end

normal3=normal2*4000


k=100
v=24/k
    c10=zeros(k,1)
    for r=1:t
        c=w10(r)
        m=-12+v
        n=-12
        for g=1:k
        if (c<m & c>=n) then
            c10(g)=c10(g)+1
            m=m+v
            n=n+v
        else
            m=m+v
            n=n+v
        end
        end
    end
c20=zeros(k,1)
c20(1)=-12+(12/k)
for b=2:k
    c20(b)=c20(b-1)+v
end
y = stdev(w10)

normal10=zeros(k,1)
normal20=zeros(k,1)
bb=-12
bc=-12+v

for wa=1:k
    bd=(bb+bc)/2
    gauss10=(1/(y*sqrt(2*%pi)))exp(-0.5(bb/y)^2)
    gauss20=(1/(y*sqrt(2*%pi)))exp(-0.5(bc/y)^2)
    gauss30=(1/(y*sqrt(2*%pi)))exp(-0.5(bd/y)^2)
    gauss40=((bc-bb)/6)*(gauss10+gauss20+4*gauss30)
    bb=bb+v
    bc=bc+v
    normal20(wa,1)=gauss40
end

normal30=normal20*4000


k=70
v=12/k
    c100=zeros(k,1)
    for r=1:t
        c=w100(r)
        m=-6+v
        n=-6
        for g=1:k
        if (c<m & c>=n) then
            c100(g)=c100(g)+1
            m=m+v
            n=n+v
        else
            m=m+v
            n=n+v
        end
        end
    end
c200=zeros(k,1)
c200(1)=-6+(6/k)
for b=2:k
    c200(b)=c200(b-1)+v
end
y = stdev(w100)

normal100=zeros(k,1)
normal200=zeros(k,1)
bb=-6
bc=-6+v

for wa=1:k
    bd=(bb+bc)/2
    gauss100=(1/(y*sqrt(2*%pi)))exp(-0.5(bb/y)^2)
    gauss200=(1/(y*sqrt(2*%pi)))exp(-0.5(bc/y)^2)
    gauss300=(1/(y*sqrt(2*%pi)))exp(-0.5(bd/y)^2)
    gauss400=((bc-bb)/6)*(gauss100+gauss200+4*gauss300)
    bb=bb+v
    bc=bc+v
    normal200(wa,1)=gauss400
end

normal300=normal200*4000
bar(c20,c10,1.0,'white')
plot(c20, normal30, 'b-')

bar(c2,c1,1.0,'white')
plot(c2, normal3, 'r-')

bar(c200,c100,1.0,'white')
plot(c200, normal300, 'm-')

    poly1.thickness=3;
    xlabel(["x / um"]);
    ylabel("molecules");
    gcf().axes_size=[500,500]
    a=gca();
    a.zoom_box=[-12,12;0,600];
    a.font_size=4;
    a.labels_font_size=5;
    a.x_label.font_size = 5;
    a.y_label.font_size = 5;
    ticks = a.x_ticks
    ticks.labels =["-12";"-10";"-8";"-6";"-4";"-2";"0";"2";"4";"6";"8";"10";"12"]
    ticks.locations = [-12;-10;-8;-6;-4;-2;0;2;4;6;8;10;12]
    a.x_ticks = ticks

endfunction

您的每个 gauss 变量都在两个地方缺少乘法运算符。检查每一行 运行。例如,这个:

gauss1=(1/(y*sqrt(2*%pi)))exp(-0.5(bb/y)^2)

应该是这样的:

gauss1=(1/(y*sqrt(2*%pi))) * exp(-0.5 * (bb/y)^2)

至于高斯钟,Scilab中没有标准函数。但是,您可以定义一个新函数以使您的情况更清楚:

function x = myGauss(s,b_)
    x = (1/(s*sqrt(2*%pi)))*exp(-0.5*(b_/s)^2)
endfunction

实际上,当我们这样做时,您的整个代码真的很难阅读。你应该定义函数而不是重复代码:它有助于阐明你的意思,如果有错误,你只需要修复一个地方。此外,我个人不建议您将所有内容都包含在 bla7() 之类的函数中,因为这会使调试变得更加困难。您的示例可以这样重写:

  • myGauss函数;
  • 函数w_计算w1w2w10w20w100w200
  • 函数c_计算c1c2c10c20c100c200
  • 函数normal_计算normal1normal2normal10normal20normal100normal200
  • 使用不同的输入根据需要多次调用所有四个函数以获得不同的结果。

如果你这样做,你的罐头将如下所示:

function x = myGauss(s,b_)
    x = (1 / (s * sqrt(2 * %pi))) * exp(-0.5 * (b_/s)^2);
endfunction

function [w1_,w2_] = w_(t_,l_,n_,p_)

    w1_ = zeros(t_,1);
    w2_ = zeros(t_,1);
    for I = 1 : t_
        a = (grand(n_,1,"unf",0,p_));
        x = l_ * cos(a);
        y = l_ * sin(a);
        z1 = zeros(n_,1);
        z2 = zeros(n_,1);
        for i = 2 : n_
            z1(i) = z1(i-1) + x(i);
            z2(i) = z2(i-1) + y(i);
        end
        w1_(I) = z1($);
        w2_(I) = z2($);
    end

endfunction

function [c1_,c2_] = c_(t_,k_,v_,w1_,x_)

    c1_ = zeros(k_,1)
    for r = 1 : t_
        c = w1_(r);
        m = -x_ + v_;
        n = -x_;
        for g = 1 : k_
            if (c < m & c >= n) then
                c1_(g) = c1_(g) + 1;
                m = m + v_;
                n = n + v_;
            else
                m = m + v_;
                n = n + v_;
            end
        end
    end
    c2_ = zeros(k_,1);
    c2_(1) = -x_ + (x_/k_);
    for b = 2 : k_
        c2_(b) = c2_(b-1) + v_;
    end

endfunction

function [normal1_,normal2_,normal3_] = normal_(k_,bb_,bc_,v_,w1_)

    y = stdev(w1_);

    normal1_ = zeros(k_,1);
    normal2_ = zeros(k_,1);

    for wa = 1 : k_ 
        bd_ = (bb_ + bc_) / 2;
        gauss1 = myGauss(y,bb_);
        gauss2 = myGauss(y,bc_);
        gauss3 = myGauss(y,bd_);
        gauss4 = ((bc_ - bb_) / 6) * (gauss1 + gauss2 + 4 * gauss3);
        bb_ = bb_ + v_;
        bc_ = bc_ + v_;
        normal2_(wa,1) = gauss4;
    end

    normal3_ = normal2_ * 4000;

endfunction

t = 4000;
l = 0.067;
p = 2 * %pi;

n = 1000;
k = 70;
v = 12 / k;
x = 6;
bb = -x;
bc = -x + v;
[w1,w2] = w_(t,l,n,p);
[c1,c2] = c_(t,k,v,w1,x);
[normal1,normal2,normal3] = normal_(k,bb,bc,v,w1);
bar(c2,c1,1.0,'white');
plot(c2, normal3, 'r-');

n = 10000;
k = 100;
v = 24 / k;
x = 12;
bb = -x;
bc = -x + v;
[w10,w20] = w_(t,l,n,p);
[c10,c20] = c_(t,k,v,w10,x);
[normal10,normal20,normal30] = normal_(k,bb,bc,v,w10);
bar(c20,c10,1.0,'white');
plot(c20, normal30, 'b-');

n = 100;
k = 70;
v = 12 / k;
x = 6;
bb = -x;
bc = -x + v;
[w100,w200] = w_(t,l,n,p);
[c100,c200] = c_(t,k,v,w100,x);
[normal100,normal200,normal300] = normal_(k,bb,bc,v,w100);
bar(c200,c100,1.0,'white');
plot(c200, normal300, 'm-');

poly1.thickness=3;
xlabel(["x / um"]);
ylabel("molecules");
gcf().axes_size=[500,500]
a=gca();
a.zoom_box=[-12,12;0,600];
a.font_size=4;
a.labels_font_size=5;
a.x_label.font_size = 5;
a.y_label.font_size = 5;
ticks = a.x_ticks
ticks.labels =["-12";"-10";"-8";"-6";"-4";"-2";"0";"2";"4";"6";"8";"10";"12"]
ticks.locations = [-12;-10;-8;-6;-4;-2;0;2;4;6;8;10;12]
a.x_ticks = ticks