在Matlab R2013b中将符号表达式转换为函数句柄

时间:2014-12-02 14:26:30

标签: matlab equation

我正在努力将我的土木工程项目所需的功能转换为符号表达。我需要使用fzero来查找函数的根。这里H应该是变量,我需要找出H的值。功能就像

function x_c = f_x_c(s,H0,VA,Lo,qc,EAo,NF,Sj,Fj)

if (s < 0) || (s > Lo)
    disp('The value of s is invalid')
    disp(['s = ' num2str(s)]);
    return
end


C1 = H/qc;
if NF == 0
    n = 0;
    sn = 0;
    sum_Fj = 0;
end


if NF >= 1
    Sj_Q = [0; Sj; Lo];
    %Determine n and sn if 0 <= s < Lo:
    if s < Lo
        STOP = 0;
        k = 0;
        while STOP == 0
            k = k + 1;
            if (s >= Sj_Q(k,1)) && (s < Sj_Q((k + 1),1))
                STOP = 1;
            end
        end
        n = k - 1;
        sn = Sj_Q(k,1);
    end
    %Determine n and sn if s = Lo:
    if s == Lo
        n = NF;
        sn = Sj(NF,1);
    end
    sum_Fj = sum(Fj(1:n,1));
end


x_c = (H/EAo)*s;
x_c = x_c + C1*asinh((qc*s - VA + sum_Fj)/H) + ...
    - C1*asinh((qc*sn - VA + sum_Fj)/H);


for j = 1:n
    sk = Sj_Q((j + 1),1);
    sk_1 = Sj_Q(j,1);
    sum_Fj = sum(Fj(1:(j - 1)));


    x_c = x_c + ...
        + C1*asinh((qc*sk - VA + sum_Fj)/H) + ...
        - C1*asinh((qc*sk_1 - VA + sum_Fj)/H);

end

我想在主文件中使用这个f_x_c.m文件,在那里我将找到这个等式的根。 有人可以指导我如何做到这一点?

我尝试过使用以下代码,但我没有成功。

if (s < 0) || (s > Lo)
    disp('The value of s is invalid')
    disp(['s = ' num2str(s)]);
    return
end

C1 = @(H) (H/qc);
if NF == 0
    n = 0;
    sn = 0;
    sum_Fj = 0;
end


if NF >= 1
    Sj_Q = [0; Sj; Lo];
    %Determine n and sn if 0 <= s < Lo:
    if s < Lo
        STOP = 0;
        k = 0;
        while STOP == 0
            k = k + 1;
            if (s >= Sj_Q(k,1)) && (s < Sj_Q((k + 1),1))
                STOP = 1;
            end
        end
        n = k - 1;
        sn = Sj_Q(k,1);
    end
    %Determine n and sn if s = Lo:
    if s == Lo
        n = NF;
        sn = Sj(NF,1);
    end
    sum_Fj = sum(Fj(1:n,1));
end


x_c =@(H) (H/EAo)*s;
x_c =@(H) (x_c(H) + (C1(H))*asinh((qc*s - VA + sum_Fj)/H) + ...
    - (C1(H))*asinh((qc*sn - VA + sum_Fj)/H));


for j = 1:n
    sk = Sj_Q((j + 1),1);
    sk_1 = Sj_Q(j,1);
    sum_Fj = sum(Fj(1:(j - 1)));


    x_c =@(H) (x_c(H) + ...
        + C1(H)*asinh((qc*sk - VA + sum_Fj)/H) + ...
        - C1(H)*asinh((qc*sk_1 - VA + sum_Fj)/H));

end

修改

我想在主文件中解决以下等式:

equation = f_x_c(inext_length, H0, vertical_reaction, inext_length, qc, EAo, NF, hanger_arc_length, point_hanger_force) + 1400;
% Whatever equation f_x_c returns, I have to add another number to it(like here it is 1400), then solve this equation using fzero.

所以,在主文件中,我写道:

equation = @(H) f_x_c(inext_length, H0, vertical_reaction, inext_length, qc, EAo, NF, hanger_arc_length, point_hanger_force);

equation = @(H) (equation(H) + 1400);
answer = fsolve(equation, H0);

1 个答案:

答案 0 :(得分:2)

你的问题的模拟答案可能看起来像

function x_c = f_x_c(H,A,B,C,D)
    x_c = H*A;
    x_c = x_c + B*asinh(C/H) - B*asinh(D/H);
end

并且对求解器的调用是

H = fzero(@(H)(f_x_c(H,1,1,1,1)+1400),1);