MATLAB: How can i solve two non-linear equations with variables from main file

for loopfsolvenon-linear equationvariable inputs

Hello. I am trying to solve two non-lienear equations using FSOLVE. These are my equations:
function F = myfun( x , coord , connect , rG , coord_mesh , i , j )
F(1) = 2*(1-x(1)-x(2))*(0.5-x(1)-x(2))*coord(connect(i,1),1)+2*x(1)*(x(1)-0.5)*coord(connect(i,2),1)+2*x(2)*(x(2)-0.5)*coord(connect(i,3),1)+4*(1-x(1)-x(2))*x(1)*coord(connect(i,4),1)+4*x(1)*x(2)*coord(connect(i,5),1)+4*x(2)*(1-x(1)-x(2))*coord(connect(i,6),1)-coord_mesh(rG(j),1);
F(2) = 2*(1-x(1)-x(2))*(0.5-x(1)-x(2))*coord(connect(i,1),2)+2*x(1)*(x(1)-0.5)*coord(connect(i,2),2)+2*x(2)*(x(2)-0.5)*coord(connect(i,3),2)+4*(1-x(1)-x(2))*x(1)*coord(connect(i,4),2)+4*x(1)*x(2)*coord(connect(i,5),2)+4*x(2)*(1-x(1)-x(2))*coord(connect(i,6),2)-coord_mesh(rG(j),2);
end
and in main file:
%explanation: coord,connect, rG and coord_mesh are matrix in main code that instead of every i and j we will be have an array
for i=1:nelem
....
....
for j=1:size(rG,1)
....
....
fun = @myfun;
x0 = [0,0];
x = fsolve(fun,x0);
I want the first one (myfun) to get all inputs (which will be changed in every loop of main code) and solve the function which is a function of x(vector of 2 variables). I don't know what format I should use for the Fsolve. Please advise. Thank you very much.

Best Answer

The fsolve function solves only for a vector of unknowns, here obviously ‘x’.
See if changing the fun assignment to this:
fun = @(x) myfun( x , coord , connect , rG , coord_mesh , i , j );
and saving the estimated parameters as a cell array:
x{i,j} = fsolve(fun,x0);
works. (You do not have to use a cell array, but since I do not know the size of ‘x’, I am defaulting to the cell array.)