Solving a nonlinear equation with time-varying parameters

조회 수: 4 (최근 30일)
ektor
ektor 2017년 11월 1일
답변: Star Strider 2017년 11월 1일
Dear all,
I want to solve this nonlinear equation: (a*b*exp(-c*d))*exp(-x)+2*b -2*x-b=0,
where in each iteration of the algorithm the a,b,c,d change values.
So I set up the following algorithm
Myfun = @(x, a, b,c,d) (a*b*exp(-c*d))*exp(-x)+2*b -2*x-b;
fun = @(x) Myfun(x, a, b,c,d);
x0=0;
options = optimoptions('fsolve','Display','none');
A = fsolve(fun,x0,options);
Is this code correct the way I have written it? Is there an alternative faster way to solve this problem? Because I noticed that the above algorithm is very slow.
Thanks in advance

채택된 답변

Star Strider
Star Strider 2017년 11월 1일
Since you are solving for a single parameter, the fzero function is an option, and in this simulation, faster:
v = mat2cell(rand(1000,4), ones(1,1000), ones(1,4)); % Create Variable Cell Array
Myfun = @(x, a, b,c,d) (a*b*exp(-c*d))*exp(-x)+2*b -2*x-b;
options = optimoptions('fsolve','Display','none');
t00 = clock;
for k1 = 1:1000
[a,b,c,d] = v{k1,:};
A0(k1) = fsolve(@(x)Myfun(x, a, b,c,d),1,options);
end
t01 = etime(clock, t00);
t10 = clock;
for k1 = 1:1000
[a,b,c,d] = v{k1,:};
A1(k1) = fzero(@(x)Myfun(x, a, b,c,d),1);
end
t11 = etime(clock, t10);
fprintf(1,'\n\tfsolve time = %.3f s\n\tfzero time = %.3f s\n\n',t01, t11)
fsolve time = 5.001 s
fzero time = 1.062 s

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Optimization Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by