Converting symbolic expression to function handle in Matlab R2013b

浪尽此生 提交于 2019-12-11 19:56:53

问题


I am struggling to convert a function, which I require for my civil engineering project, from symbolic expression. I need to use fzero to find the root of the function. Here H should be the variable and I need to find out the value of H. The function goes like

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

I want to use this f_x_c.m file in the main file where I will find the roots of this equation. Could someone guide me how I can do that?

I have tried doing it using the following code but I wasn't successful.

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

Edit:

I want to solve the following equation in the main file:

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.

So, in the main file, I wrote like:

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:


A mock answer to your question probably looks like

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

and a call to solver is

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


来源:https://stackoverflow.com/questions/27251770/converting-symbolic-expression-to-function-handle-in-matlab-r2013b

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!