Change equation in fsolve inside a loop

조회 수: 1 (최근 30일)
esbolico
esbolico 2012년 11월 5일
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!!

채택된 답변

Conrad
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))];

추가 답변 (2개)

esbolico
esbolico 2012년 11월 5일
Hi Conrad,
thank you for your answer. I'll check it and keep you informed!!!

esbolico
esbolico 2012년 11월 5일
I still do not know how to make it work. The thing is I get my model as a function of x1,x2,x3 or x,y,z. How can I change it to get x(1),x(2),x(3)??
I'll try to explain it better:
After running my code I get a symbolic variable such as
EqsToSolve= [x1+x2*x2+7*x3 ;log(x1)+exp(x2)+x3 ;3*x1+x2*x3+2]
as far as I understand you are telling me to change manually x1-->x(1), x2-->x(2) and so on

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by