MATLAB: Change equation in fsolve inside a loop

for loopfsolvesymswhile

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!!

Best Answer

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