Change equation in fsolve inside a loop
1 次查看(过去 30 天)
显示 更早的评论
Hi All,
I am having some trouble with the following problem:
I create some symbolic equations from a complex mathematical model (the following equations are just to show my problem) as follows:
syms x1 x2 x3
EqsToSolve= [x1+x2*x2+7*x3 ;log(x1)+exp(x2)+x3 ;3*x1+x2*x3+2]
what I do now is copying this to a M-File that looks like
function y=myfun(x)
x1=x(1)
x2=x(2)
x3=x(3)
y=[x1+x2*x2+7*x3 ;log(x1)+exp(x2)+x3 ;3*x1+x2*x3+2]
end
and I solve it using fsolve as usual.
With the solution of this system I update my model and create another set of equations such as:
EqsToSolve2= [54*x1^2+x2+x3 ;(x2)+x3+32 ;3*x1+x2*x3+2+log(x3)]
and I copy that to myfun.m and so on till convergence. The problem is that convergence can arrive after 1000 iterations and this procedure is slow.
IS there any way to solve this problem, that is, to achieve something like:
while not_converged
create_updated_model
solve new_equation
end
or how to change the equation in the myfun.m inside a while o for loop.
Thank you very much!! Any help will be appreciated!!
0 个评论
采纳的回答
Conrad
2012-11-5
Try using anonymous functions:
f1 = @(x) [x(1)+x(2)*x(2)+7*x(3) ;log(x(1))+exp(x(2))+x(3);3*x(1)+x(2)*x(3)+2];
f2 = @(x) [54*x(1)^2+x(2)+x(3) ;(x(2))+x(3)+32 ;3*x(1)+x(2)*x(3)+2+log(x(3))];
0 个评论
更多回答(2 个)
esbolico
2012-11-5
2 个评论
Conrad
2012-11-7
f1 = @(x1,x2,x3) [x1+x2*x2+7*x3 ;log(x1)+exp(x2)+x3;3*x1+x2*x3+2];
Walter Roberson
2012-11-7
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Symbolic Math Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!