Solve not returning values after first iteration?
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Omega=zeros(181,2)clear
clc
%Lengths of links
r1 = 7.5; r2 = 2.9; r3 = 10.85; r4 = 6.2;
%Angle between two grounds
theta1 = deg2rad(27);
%Starting angle of link 2
theta2deg=-68;
%Reference angles for links 3 and 4
theta3g=12;
theta4g=-37;
a=0
Angle=zeros(181,3);
theta2temp=theta2deg
%Counting loops until colinear
while true
a=a+1
theta2temp=theta2temp+2
if theta2temp>rad2deg(theta1)
break
end
end
%Loop for angles of all links until colinear
for i=1:a
theta2new=theta2deg+2*(i-1)
theta2=deg2rad(theta2new);
f = @(x) [ ...
r2*cos(theta2) + r3*cos(x(1)) - r4*cos(x(2)) - r1*cos(theta1);
r2*sin(theta2) + r3*sin(x(1)) - r4*sin(x(2)) - r1*sin(theta1)
];
initial_guess = [deg2rad(theta3g), deg2rad(theta4g)];
options = optimoptions('fsolve','Display','iter');
solution = fsolve(f, initial_guess, options);
theta3 = rad2deg(solution(1));
theta4 = rad2deg(solution(2));
fprintf('Theta3 = %.2f degrees\n', theta3);
fprintf('Theta4 = %.2f degrees\n', theta4);
theta3g=theta3;
theta4g=theta4;
Angle(i,:)=[theta2new,theta3,theta4];
end
%Angles of all links after colinear (angles should be shrinking)
for i=a:181
theta2new=theta2deg+2*(i-1);
theta2=deg2rad(theta2new);
f = @(x) [ ...
r2*cos(theta2) + r3*cos(x(1)) - r4*cos(x(2)) - r1*cos(theta1);
r2*sin(theta2) + r3*sin(x(1)) - r4*sin(x(2)) - r1*sin(theta1)
];
%Angular velocity is greatest here, therefore the guesses must be adjusted
%closer to the desired value to reach it
initial_guess = [(deg2rad(theta3g-5)), (deg2rad(theta4g-5))];
options = optimoptions('fsolve','Display','iter');
solution = fsolve(f, initial_guess, options);
theta3 = rad2deg(solution(1));
theta4 = rad2deg(solution(2));
fprintf('Theta3 = %.2f degrees\n', theta3);
fprintf('Theta4 = %.2f degrees\n', theta4);
theta3g=theta3;
theta4g=theta4;
Angle(i,:)=[theta2new,theta3,theta4];
end
Angle
for a=1:181
hold on
Bx=r4*cos(deg2rad(Angle(a,3)));
By=r4*sin(deg2rad(Angle(a,3)));
plot(Bx,By,'ro')
Ax=r2*cos(deg2rad(Angle(a,1)));
Ay=r2*sin(deg2rad(Angle(a,1)));
plot(Ax,Ay,'bo')
end
Omega=zeros(181,2);
syms omega3 omega4
for j=1:181
a=r3*cos(deg2rad(Angle(j,2)+90));
b=r4*cos(deg2rad(Angle(j,3)+90));
c=r2*cos(deg2rad(Angle(j,1)+90));
d=r3*sin(deg2rad(Angle(j,2)+90));
e=r4*sin(deg2rad(Angle(j,3)+90));
f=r2*sin(deg2rad(Angle(j,1)+90));
eq1 = omega3*a == omega4*b - 100*c;
eq2 = omega3*d == omega4*e - 100*f ;
% Solve the system
sol = solve([eq1, eq2], [omega3, omega4]);
omega3=double(sol.omega3);
omega4=double(sol.omega4);
Omega(j,1)=omega3
Omega(j,2)=omega4
end
% Store solution in a temporary matrix
%
%
%
%
% My only issue is with the final for loop, I created it in a seperate
% window and got the correct values and now I put it in a loop and the
% inputs are from values I derived earlier (which are correct) yet the loop
% is only returning zeroes and isn't working properly. How can I make a
% loop that solves for the same two equations and two unknowns at different
% inputs and puts all the outputs in a matrix? Thanks!
0 Kommentare
Antworten (1)
Torsten
am 15 Apr. 2025
Bearbeitet: Torsten
am 15 Apr. 2025
You overwrite omega3 and omega4 as symbolic variables with numerical values.
Replace
% Solve the system
sol = solve([eq1, eq2], [omega3, omega4]);
omega3=double(sol.omega3);
omega4=double(sol.omega4);
Omega(j,1)=omega3
Omega(j,2)=omega4
by
% Solve the system
sol = solve([eq1, eq2], [omega3, omega4]);
Omega(j,1)=double(sol.omega3);
Omega(j,2)=double(sol.omega4);
0 Kommentare
Siehe auch
Kategorien
Mehr zu Symbolic Computations in MATLAB finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!