Can't use fsolve to solve system of equations (4 unknowns)
4 views (last 30 days)
Show older comments
I'm attempting to solve a system of equations, 4 equations and 4 unkowns. All known values are constants outside of phi2 (array from 0 to 2pi rad). I made all my other known constants arrays of length(phi2) Here is my code:
phi2 = [1:360]*(pi/180);
phi = pi/2;
r2 = 0.3; r2 = linspace(r2,r2,length(phi2));
rBo = 0.75; rBo = linspace(rBo,rBo,length(phi2));
phiBo = (360-106)*(pi/180); phiBo = linspace(phiBo,phiBo,length(phi2));
r5 = 0.2; r5 = linspace(r5,r5,length(phi2));
r4 = 1.4; r4 = linspace(r4,r4,length(phi2));
ry = 0.5; ry = linspace(ry,ry,length(phi2));
f = @(x) [(r2.*cos(phi2)) + (x(1).*cos(x(2))) - x(3);
(r2.*sin(phi2)) + (x(1).*sin(x(2))) - ry;
(rBo.*cos(phiBo))+(r5.*cos(x(4))) + (r4.*cos(x(2))) - x(3);
(rBo.*sin(phiBo)) + (r5.*sin(x(4))) + (r4.*sin(x(2))) - ry;]
x0 = [.9 pi/3 .7 pi/6];
x0 = [.6 .6 .6 .6];
X = fsolve(f,x0)
I've tried multiple guess values (two sets shown above) and can't get fsolve to solve for x(1) x(2) x(3) and x(4). Depending on my guess values I end up with the two errors below and I'm not sure why.
No solution found.
fsolve stopped because the last step was ineffective. However, the vector of function
values is not near zero, as measured by the value of the function tolerance.
<stopping criteria details>
X =
0.5746 1.0556 0.2831 3.1281
or
No solution found.
fsolve stopped because the problem appears regular as measured by the gradient,
but the vector of function values is not near zero as measured by the
value of the function tolerance.
<stopping criteria details>
X =
0.5746 1.0556 0.2831 3.1281
I'm trying to solve for four arrays of length phi2. I've been told to utilize fsolve in doing this. Any insight would be highly appreciated.
0 Comments
Answers (1)
Davide Masiello
on 29 Mar 2022
Edited: Davide Masiello
on 29 Mar 2022
I would suggest you put everything in a loop where at each iteration you solve for a different values of phi2.
Moreover, at each iteration you can use the values of X found at the previous one as new initial guess.
clear,clc
phi2 = (1:360)*(pi/180);
phi = pi/2;
r2 = 0.3;
rBo = 0.75;
phiBo = (360-106)*(pi/180);
r5 = 0.2;
r4 = 1.4;
ry = 0.5;
X = zeros(4,length(phi2));
options = optimoptions('fsolve','Display','off');
for idx = 1:length(phi2)
f = @(x) [ (r2.*cos(phi2(idx)))+(x(1).*cos(x(2)))-x(3) ;...
(r2.*sin(phi2(idx)))+(x(1).*sin(x(2)))-ry ;...
(rBo.*cos(phiBo))+(r5.*cos(x(4)))+(r4.*cos(x(2)))-x(3) ;...
(rBo.*sin(phiBo))+(r5.*sin(x(4)))+(r4.*sin(x(2)))-ry ;...
] ;
if idx == 1
x0 = [.9 pi/3 .7 pi/6];
else
x0 = X(:,idx-1);
end
X(:,idx) = fsolve(f,x0,options);
end
plot(phi2,X)
xlabel('phi2')
ylabel('X')
legend('x1','x2','x3','x4')
2 Comments
Davide Masiello
on 30 Mar 2022
The reason why you can't use fsolve outside the loop is that phi2 is an array. Therefore, each expression where phi2 appears becomes a set of N equations, where N is the length of the phi2 array.
In the loop, the system is solved at each iteration for a single value of phi2, hence it works.
Let me know if this makes it clear.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!