# a system of non-linear equations (using fsolve in a loop)

4 views (last 30 days)
Mahsa Babaee on 30 Apr 2021
Commented: Star Strider on 30 Apr 2021
Hi friends,
I am trying to solve a system of equations of 5 equations and 5 variables. Equations are non-linear ( containing quadratic function, sine, and cosine functions).
How can I change some of the equation parameters ( b1, b2, b3, g1, g2, g3 in terms of n , m) ,using a loop, and solve the system of equations by "fsolve" each time?
My code is as following but there is an error as: "Unrecognized function or variable 'n' ". what should I do now???
How should I introduce n, m to both m-files?? Where should I apply the for- loop?
function G= fsolve_trial(X)
% Parameters:%
k1 = 0.25; k2 = 0.5; k3 = 0.2;
Lx = 40e-03;
Ly = 40e-03;
Lz1 = 0.08e-03; Lz2 = 2e-03; Lz3 = 10e-03;
a1 = 5.79e-08; a2 = 1.23e-07; a3 = 6.67e-08;
b1 = (n*pi/Lx)^2;
b2 = (n*pi/Lx)^2;
b3 = (n*pi/Lx)^2;
g1 = (m*pi/Ly)^2;
g2 = (m*pi/Ly)^2;
g3 = (m*pi/Ly)^2;
w1 = 0; w2 = 101; w3 = 137;
% Variable Vector:%
s1= X(1);
s2= X(2);
s3= X(3);
s21= X(4);
s32= X(5);
% Equations:%
eq1=k2*k3*s21*s32*cos(Lz1*s1)*cos(Lz2*((a1*s1^2 + a1*b1 - a2*b2 + a1*g1 - a2*g2 + a1*w1 - a2*w2)/a2)^(1/2))*cos(Lz3*((a1*s1^2 + a1*b1 - a3*b3 + a1*g1 - a3*g3 + a1*w1 - a3*w3)/a3)^(1/2)) - k2*s21*cos(Lz1*s1)*sin(Lz2*((a1*s1^2 + a1*b1 - a2*b2 + a1*g1 - a2*g2 + a1*w1 - a2*w2)/a2)^(1/2))*sin(Lz3*((a1*s1^2 + a1*b1 - a3*b3 + a1*g1 - a3*g3 + a1*w1 - a3*w3)/a3)^(1/2)) - k3*s32*cos(Lz3*((a1*s1^2 + a1*b1 - a3*b3 + a1*g1 - a3*g3 + a1*w1 - a3*w3)/a3)^(1/2))*sin(Lz1*s1)*sin(Lz2*((a1*s1^2 + a1*b1 - a2*b2 + a1*g1 - a2*g2 + a1*w1 - a2*w2)/a2)^(1/2)) - cos(Lz2*((a1*s1^2 + a1*b1 - a2*b2 + a1*g1 - a2*g2 + a1*w1 - a2*w2)/a2)^(1/2))*sin(Lz1*s1)*sin(Lz3*((a1*s1^2 + a1*b1 - a3*b3 + a1*g1 - a3*g3 + a1*w1 - a3*w3)/a3)^(1/2));
eq2= s2-sqrt((a1/a2)*(b1+g1+s1^2+w1)-(b2+g2+w2));
eq3= s3-sqrt((a1/a3)*(b1+g1+s1^2+w1)-(b3+g3+w3));
eq4= s21- (s2/s1);
eq5= s32- (s3/s2);
G= [eq1; eq2; eq3; eq4; eq5];
%%%%%%%%%%%%%%%%%%%%
And:
% Crating Solved Variable Matrix: %
ss1= zeros(10, 10);
ss2= zeros(10, 10);
ss3= zeros(10, 10);
for n=1:10
for m=1:10
fhandle= @fsolve_trial;
X0= [120 12 100 0.1 8];
X= fsolve( fhandle, X0);
ss1(n,m)=X(1);
ss2(n,m)=X(2);
ss3(n,m)=X(3);
end
end

Star Strider on 30 Apr 2021
Edited: Star Strider on 30 Apr 2021
Pass them as extra parameters. See Passing Extra Parameters for details.
This appears to work —
% Crating Solved Variable Matrix: %
ss1= zeros(10, 10);
ss2= zeros(10, 10);
ss3= zeros(10, 10);
for n=1:10
for m=1:10
fhandle= @(X)fsolve_trial(X,n,m);
X0= [120 12 100 0.1 8];
X = fsolve( fhandle, X0);
ss1(n,m)=X(1);
ss2(n,m)=X(2);
ss3(n,m)=X(3);
end
end
ss1
ss1 =
1.0e+02 * 1.4043 + 0.0000i 1.7696 + 0.0000i 2.2220 + 0.0000i 2.1548 + 0.0000i 3.0841 + 0.0000i 2.4868 + 0.0000i 2.9175 + 0.0000i 3.9615 + 0.0000i 4.2005 + 0.0000i 3.0797 + 0.0000i 1.7696 + 0.0000i 2.0579 + 0.0000i 2.4366 + 0.0000i 2.8305 + 0.0000i 2.1634 + 0.0000i 3.1982 + 0.0000i 3.7637 + 0.0000i 4.0065 + 0.0000i 2.8257 + 0.0000i 3.1251 + 0.0000i 2.2220 + 0.0000i 2.4366 + 0.0000i 2.7306 + 0.0000i 3.0460 + 0.0000i 2.4983 + 0.0000i 3.1816 + 0.0000i 3.8501 + 0.0000i 4.0785 + 0.0000i 4.3026 + 0.0000i 3.1992 + 0.0000i 2.1548 + 0.0000i 2.8305 + 0.0000i 3.0460 + 0.0000i 2.5124 + 0.0000i 3.2059 + 0.0000i 3.7456 + 0.0000i 3.9615 + 0.0000i 2.7415 + 0.0000i 4.3879 + 0.0000i 4.6057 + 0.0000i 3.0841 + 0.0000i 2.1634 + 0.0000i 2.4983 + 0.0000i 3.2059 + 0.0000i 2.9175 + 0.0000i 3.8991 + 0.0000i 4.0925 + 0.0000i 4.2901 + 0.0000i 3.1549 + 0.0000i 4.7031 + 0.0000i 2.4868 + 0.0000i 3.1982 + 0.0000i 3.1816 + 0.0000i 3.7456 + 0.0000i 3.8991 + 0.0000i 4.0643 + 0.0000i 2.8257 + 0.0000i 3.0645 + 0.0000i 3.3143 + 0.0000i 4.8183 + 0.0000i 2.9175 + 0.0000i 3.7637 + 0.0000i 3.8501 + 0.0000i 3.9615 + 0.0000i 4.0925 + 0.0000i 2.8257 + 0.0000i 4.3999 + 0.0000i 4.5724 + 0.0000i 3.4934 + 0.0000i 4.9497 + 0.0000i 3.9615 + 0.0000i 4.0065 + 0.0000i 4.0785 + 0.0000i 2.7415 + 0.0000i 4.2901 + 0.0000i 3.0645 + 0.0000i 4.5724 + 0.0000i 4.7383 + 0.0000i 4.9098 + 0.0000i 5.0957 + 0.0000i 4.2005 + 0.0000i 2.8257 + 0.0000i 4.3026 + 0.0000i 4.3879 + 0.0000i 3.1549 + 0.0000i 3.3143 + 0.0000i 3.4934 + 0.0000i 4.9098 + 0.0000i 5.0766 + 0.0000i 5.2551 + 0.0000i 3.0797 + 0.0000i 3.1251 + 0.0000i 3.1992 + 0.0000i 4.6057 + 0.0000i 4.7031 + 0.0000i 4.8183 + 0.0000i 4.9497 + 0.0000i 5.0957 + 0.0000i 5.2551 + 0.0000i 5.4269 + 0.0000i
ss2
ss2 =
1.0e+02 * 0.5151 + 0.0000i 0.0000 + 0.4103i 0.0000 + 0.9751i 0.0005 + 1.8378i 0.0000 + 2.0053i 0.0121 + 2.9317i 0.0865 + 3.1562i 0.0000 + 3.7207i -0.0000 + 4.2983i -0.0000 + 5.3404i 0.0000 + 0.4103i 0.0000 + 0.7927i 0.0000 + 1.2081i 0.0000 + 1.6638i 0.0031 + 2.6908i 0.0871 + 2.5576i 0.0000 + 3.2627i 0.0000 + 3.8281i -0.0000 + 4.8992i -0.0000 + 5.4191i 0.0000 + 0.9751i 0.0000 + 1.2081i 0.0000 + 1.5417i 0.0000 + 1.9505i 0.0017 + 2.7808i 0.0992 + 2.7331i 0.0000 + 3.4595i 0.0000 + 4.0016i 0.0000 + 4.5474i -0.0000 + 5.5478i 0.0005 + 1.8378i 0.0000 + 1.6638i 0.0000 + 1.9505i -0.0050 + 2.6887i 0.0874 + 2.5754i 0.0000 + 3.2222i 0.0000 + 3.7207i -0.0000 + 4.7529i 0.0000 + 4.7555i 0.0000 + 5.2817i 0.0000 + 2.0053i 0.0031 + 2.6908i 0.0017 + 2.7808i 0.0874 + 2.5754i 0.0865 + 3.1562i 0.0000 + 3.5734i 0.0000 + 4.0355i 0.0000 + 4.5170i -0.0000 + 5.4710i 0.0000 + 5.5143i 0.0121 + 2.9317i 0.0871 + 2.5576i 0.0992 + 2.7331i 0.0000 + 3.2222i 0.0000 + 3.5734i 0.0000 + 3.9674i -0.0000 + 4.8992i -0.0000 + 5.3139i -0.0000 + 5.7478i 0.0000 + 5.7864i 0.0865 + 3.1562i 0.0000 + 3.2627i 0.0000 + 3.4595i 0.0000 + 3.7207i 0.0000 + 4.0355i -0.0000 + 4.8992i 0.0000 + 4.7846i 0.0000 + 5.2020i -0.0000 + 6.0587i 0.0000 + 6.0929i 0.0000 + 3.7207i 0.0000 + 3.8281i 0.0000 + 4.0016i -0.0000 + 4.7529i 0.0000 + 4.5170i -0.0000 + 5.3139i 0.0000 + 5.2020i 0.0004 + 5.5895i 0.0000 + 6.0002i 0.0000 + 6.4288i -0.0000 + 4.2983i -0.0000 + 4.8992i 0.0000 + 4.5474i 0.0000 + 4.7555i -0.0000 + 5.4710i -0.0000 + 5.7478i -0.0000 + 6.0587i 0.0000 + 6.0002i 0.0000 + 6.3850i 0.0000 + 6.7899i -0.0000 + 5.3404i -0.0000 + 5.4191i -0.0000 + 5.5478i 0.0000 + 5.2817i 0.0000 + 5.5143i 0.0000 + 5.7864i 0.0000 + 6.0929i 0.0000 + 6.4288i 0.0000 + 6.7899i 0.0000 + 7.1723i
ss3
ss3 =
1.0e+02 * 1.2391 + 0.0000i 1.5158 + 0.0000i 1.8596 + 0.0000i 1.6232 - 0.0002i 2.4753 + 0.0000i 1.5393 - 0.0791i 1.5978 - 0.0767i 2.8843 + 0.0000i 2.9376 - 0.0000i 0.0002 + 0.0001i 1.5158 + 0.0000i 1.7354 + 0.0000i 2.0204 + 0.0000i 2.3051 + 0.0000i 1.2876 - 0.0392i 1.8964 - 0.6008i 2.8231 + 0.0000i 2.8959 + 0.0000i 0.0001 + 0.0002i 0.0002 + 0.0000i 1.8596 + 0.0000i 2.0204 + 0.0000i 2.2347 + 0.0000i 2.4507 + 0.0000i 1.6632 - 0.1738i 1.6083 - 0.5041i 2.8520 + 0.0000i 2.9129 + 0.0000i 2.9549 + 0.0000i 0.0002 + 0.0000i 1.6232 - 0.0002i 2.3051 + 0.0000i 2.4507 + 0.0000i 1.7473 - 0.2232i 1.8318 - 0.6176i 2.8166 + 0.0000i 2.8843 + 0.0000i 0.0002 + 0.0000i 2.9675 + 0.0000i 2.9932 + 0.0000i 2.4753 + 0.0000i 1.2876 - 0.0392i 1.6632 - 0.1738i 1.8318 - 0.6176i 1.5978 - 0.0767i 2.8669 + 0.0000i 2.9159 + 0.0000i 2.9529 + 0.0000i 0.0002 + 0.0000i 3.0024 + 0.0000i 1.5393 - 0.0791i 1.8964 - 0.6008i 1.6083 - 0.5041i 2.8166 + 0.0000i 2.8669 + 0.0000i 2.9097 + 0.0000i 0.0001 + 0.0002i 0.0002 + 0.0001i 0.0002 + 0.0001i 3.0119 + 0.0000i 1.5978 - 0.0767i 2.8231 + 0.0000i 2.8520 + 0.0000i 2.8843 + 0.0000i 2.9159 + 0.0000i 0.0001 + 0.0002i 2.9691 + 0.0000i 2.9898 + 0.0000i 0.0001 + 0.0003i 3.0211 + 0.0000i 2.8843 + 0.0000i 2.8959 + 0.0000i 2.9129 + 0.0000i 0.0002 + 0.0000i 2.9529 + 0.0000i 0.0002 + 0.0001i 2.9898 + 0.0000i 3.0115 - 0.0007i 3.0185 + 0.0000i 3.0298 + 0.0000i 2.9376 - 0.0000i 0.0001 + 0.0002i 2.9549 + 0.0000i 2.9675 + 0.0000i 0.0002 + 0.0000i 0.0002 + 0.0001i 0.0001 + 0.0003i 3.0185 + 0.0000i 3.0288 + 0.0000i 3.0379 + 0.0000i 0.0002 + 0.0001i 0.0002 + 0.0000i 0.0002 + 0.0000i 2.9932 + 0.0000i 3.0024 + 0.0000i 3.0119 + 0.0000i 3.0211 + 0.0000i 3.0298 + 0.0000i 3.0379 + 0.0000i 3.0456 - 0.0000i
function G= fsolve_trial(X,n,m)
% Parameters:%
k1 = 0.25; k2 = 0.5; k3 = 0.2;
Lx = 40e-03;
Ly = 40e-03;
Lz1 = 0.08e-03; Lz2 = 2e-03; Lz3 = 10e-03;
a1 = 5.79e-08; a2 = 1.23e-07; a3 = 6.67e-08;
b1 = (n*pi/Lx)^2;
b2 = (n*pi/Lx)^2;
b3 = (n*pi/Lx)^2;
g1 = (m*pi/Ly)^2;
g2 = (m*pi/Ly)^2;
g3 = (m*pi/Ly)^2;
w1 = 0; w2 = 101; w3 = 137;
% Variable Vector:%
s1= X(1);
s2= X(2);
s3= X(3);
s21= X(4);
s32= X(5);
% Equations:%
eq1=k2*k3*s21*s32*cos(Lz1*s1)*cos(Lz2*((a1*s1^2 + a1*b1 - a2*b2 + a1*g1 - a2*g2 + a1*w1 - a2*w2)/a2)^(1/2))*cos(Lz3*((a1*s1^2 + a1*b1 - a3*b3 + a1*g1 - a3*g3 + a1*w1 - a3*w3)/a3)^(1/2)) - k2*s21*cos(Lz1*s1)*sin(Lz2*((a1*s1^2 + a1*b1 - a2*b2 + a1*g1 - a2*g2 + a1*w1 - a2*w2)/a2)^(1/2))*sin(Lz3*((a1*s1^2 + a1*b1 - a3*b3 + a1*g1 - a3*g3 + a1*w1 - a3*w3)/a3)^(1/2)) - k3*s32*cos(Lz3*((a1*s1^2 + a1*b1 - a3*b3 + a1*g1 - a3*g3 + a1*w1 - a3*w3)/a3)^(1/2))*sin(Lz1*s1)*sin(Lz2*((a1*s1^2 + a1*b1 - a2*b2 + a1*g1 - a2*g2 + a1*w1 - a2*w2)/a2)^(1/2)) - cos(Lz2*((a1*s1^2 + a1*b1 - a2*b2 + a1*g1 - a2*g2 + a1*w1 - a2*w2)/a2)^(1/2))*sin(Lz1*s1)*sin(Lz3*((a1*s1^2 + a1*b1 - a3*b3 + a1*g1 - a3*g3 + a1*w1 - a3*w3)/a3)^(1/2));
eq2= s2-sqrt((a1/a2)*(b1+g1+s1^2+w1)-(b2+g2+w2));
eq3= s3-sqrt((a1/a3)*(b1+g1+s1^2+w1)-(b3+g3+w3));
eq4= s21- (s2/s1);
eq5= s32- (s3/s2);
G= [eq1; eq2; eq3; eq4; eq5];
%%%%%%%%%%%%%%%%%%%%
end
.

Mahsa Babaee on 30 Apr 2021
Edited: Mahsa Babaee on 30 Apr 2021
Thank you so much for your help.
I pasted what you wrote , unfortunately still there is an error as:
>> trial_MatlabSite
Error using fsolve_trial
Too many input arguments.
Error in trial_MatlabSite>@(X)fsolve_trial(X,n,m) (line 10)
fhandle= @(X)fsolve_trial(X,n,m);
Error in fsolve (line 258)
fuser = feval(funfcn{3},x,varargin{:});
Error in trial_MatlabSite (line 12)
X = fsolve( fhandle, X0);
Caused by:
Failure in initial objective function evaluation. FSOLVE cannot continue.
I dont know what is wrong!
Star Strider on 30 Apr 2021
My pleasure!
That was a way to test the code, and should not have been included in the code I posted. (I have now deleted it.)
.