Change equation in fsolve inside a loop

1 view (last 30 days)
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!!

Accepted Answer

Conrad
Conrad on 5 Nov 2012
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))];

More Answers (2)

esbolico
esbolico on 5 Nov 2012
Hi Conrad,
thank you for your answer. I'll check it and keep you informed!!!

esbolico
esbolico on 5 Nov 2012
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

Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!