MATLAB: Unable to use fsolve for solving equations whose coefficients are changing with a for loop

fsolve loops

The main function is like
function F= root2d(x)
filename='C:\Users\Asus\Desktop\thmb\thmb.xlsx';
xlrange='A1:D501';
num=xlsread(filename,xlrange);
l21=38.5;l11=49.5;l31=31.1;
for i=1:2
dx(i)=num(i,2);
dy(i)=num(i,3);
dz(i)=num(i,4);
end
for i=1:2
F(1) = l21*cosd(x(2)+x(3))+l11*cosd(x(4))+l31*cosd(x(2)+x(3)+x(4))-dx(i)*cosd(x(1))-dz(i)*sind(x(1));
F(2) = l21*sind(x(2)+x(3))+l11*sind(x(2))+l31*sind(x(2)+x(3)+x(4))-dy(i);
F(3) = x(1)-atand(dz(i)/dx(i));
F(4) = x(2)-(10/9)*x(3);
end
end
% dx(i), dy(i) and dz(i) changes from the excel table
The solve code is as follows
clc;
clear all;
fun = @root2d;
x0 = [0 0 0 0];
x=fsolve(fun,x0);
% = fsolve(fun,x0);
end
I have to find every value of x(1),x(2),x(3),x(4) for all the equations. Tell me the necessary changes

Best Answer

Do not put the definition of fixed parameters (e.g., dx,dy,dz) inside roots2d. Pass them using methods discussed here: