My code is outputting very illogical answers for the second block (my second vector loop equation).
Show older comments
The first block of code (E1) runs and comes out with a correct answer (validated in excel). It has only one input and two outputs. The second block (E2) of code runs, but comes out with an illogical answer. This block has two inputs that are dependent on the previous block's outputs. I'm expecting the result to be some sort of sine or cosine wave as this code is the position analysis of an ornithoptor.
"fsolve" in the in E2 also says "no solution found", but provides an answer. I do not understand why.
Why won't my code work properly for the E2 block?
% given values
R1 = 59.7;
R2 = 18.8;
R3 = 41.0;
R4 = 40.1;
R5 = 136.4;
R6 = 11.9;
R7 = 15.6;
R8 = 11.7;
Rgi = 13.1;
Rec = 12.7;
Rfc = 137.2;
theta1 = 0;
theta2_all = linspace(0, 2*pi, 500); % all possible input values
% E1
for i = 1:500
VLE1 = @(x0) [(R2.*cos(theta2_all(i)) + R3.*cos(x0(1)) - R4.*cos(x0(2)) - R1.*cos(theta1));
(R2.*sin(theta2_all(i)) + R3.*sin(x0(1)) - R4.*sin(x0(2)) - R1.*sin(theta1))];
x0 = [pi/2; pi/2];
x = fsolve(VLE1, x0);
theta3(i) = x(1);
theta4(i) = x(2);
end
theta3
theta4
% E2
theta_ec_off = 16.5*(pi/180);
theta_fc_off = 8.5*(pi/180);
theta_ec = theta3 + theta_ec_off;
theta_fc = theta4 + theta_fc_off;
for i = 1:500
VLE2 = @(x0) [(Rec*cos(theta_ec(i)) + Rfc*cos(theta_fc(i)) - R6*cos(x0(2)) - R5*cos(x0(1)));
(Rec*sin(theta_ec(i)) + Rfc*sin(theta_fc(i)) - R6*cos(x0(2)) - R5*cos(x0(1)))];
x0 = [pi/2; pi/2];
x = fsolve(VLE2, x0);
theta5(i) = x(1);
theta6(i) = x(2); % storing values in a vector
end
theta5
theta6
% validating
plot(theta2_all, theta5)
hold on
plot(theta2_all, theta6)
Accepted Answer
More Answers (2)
David Goodmanson
on 1 Oct 2022
Edited: David Goodmanson
on 6 Oct 2022
Hi Emily,
If VLE1 and VLE2 are lined up typographically, you have
VLE1 = @(x0) [(R2.*cos(theta2_all(i)) + R3.*cos(x0(1)) - R4.*cos(x0(2)) - R1.*cos(theta1));
(R2.*sin(theta2_all(i)) + R3.*sin(x0(1)) - R4.*sin(x0(2)) - R1.*sin(theta1))];
VLE2 = @(x0) [(Rec*cos(theta_ec(i)) + Rfc*cos(theta_fc(i)) - R6*cos(x0(2)) - R5*cos(x0(1)));
(Rec*sin(theta_ec(i)) + Rfc*sin(theta_fc(i)) - R6*cos(x0(2)) - R5*cos(x0(1)))];
Looking at just terms involving R3 and R4, VLE1 has cosines on the first line, sines on the second line. But for R5 and R6, VLE2 has cosines on both lines, meaning that the first line and the second line have identical terms. This does not seem right.
1 Comment
Emily Friedman
on 1 Oct 2022
% given values
R1 = 59.7;
R2 = 18.8;
R3 = 41.0;
R4 = 40.1;
R5 = 136.4;
R6 = 11.9;
R7 = 15.6;
R8 = 11.7;
Rgi = 13.1;
Rec = 12.7;
Rfc = 137.2;
theta1 = 0;
theta2_all = linspace(0, 2*pi, 500); % all possible input values
options = optimset('Display','off') ;
x0 = [pi/2 pi/2];
% E1
for i = 1:500
VLE1 = @(x0) [(R2.*cos(theta2_all(i)) + R3.*cos(x0(1)) - R4.*cos(x0(2)) - R1.*cos(theta1));
(R2.*sin(theta2_all(i)) + R3.*sin(x0(1)) - R4.*sin(x0(2)) - R1.*sin(theta1))];
x = fsolve(VLE1, x0, options);
x0 = x;
theta3(i) = x(1);
theta4(i) = x(2);
end
figure(1)
hold on
plot(theta2_all, theta3)
plot(theta2_all, theta4)
hold off
% E2
theta_ec_off = 16.5*(pi/180);
theta_fc_off = 8.5*(pi/180);
theta_ec = theta3 + theta_ec_off;
theta_fc = theta4 + theta_fc_off;
x0 = [pi/2 pi/2];
for i = 1:500
VLE2 = @(x0) [(Rec*cos(theta_ec(i)) + Rfc*cos(theta_fc(i)) - R6*cos(x0(2)) - R5*cos(x0(1)));
(Rec*sin(theta_ec(i)) + Rfc*sin(theta_fc(i)) - R6*sin(x0(2)) - R5*sin(x0(1)))];
x = fsolve(VLE2, x0, options);
x0 = x;
theta5(i) = x(1);
theta6(i) = x(2); % storing values in a vector
end
figure(2)
hold on
plot(theta2_all, theta5)
plot(theta2_all, theta6)
hold off
Categories
Find more on Dates and Time in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

