Why are my plots empty?
Show older comments
clear;clc;
%Known Values
r2=140; %input
r3=70.1; %known
r4=70.1; %known
rBE=47.1; %known
r5=140; %unknown
rBC=47.1; %known
theta2=0; %known
theta3=60; %unknown
theta4=60; %unknown
os4=10; %offset from theta4
thetaBE=theta4+os4;%known
theta5=0; %unknown
os3=10; %offset from theta3
thetaBC=theta3+os3; %known
e = zeros(4,360);
%Position
while r2 <= 360
error = 1;
x = [theta3;theta4;theta5;r5]; %Matrix of Initial Guesses
tol = 1e-8;
i = 0;
while ((error > tol) && (i < 1000))
f = fun(x,r2);
j = jac(x);
deltax = j\f;
xnew = x - deltax;
error = abs ((xnew - x)) ./ xnew;
error = max(error);
x = xnew;
i = i + 1;
end
figure(1)
p=plot(r2,x(1),'r.');
xlabel('r2')
ylabel('1')
hold on
figure(2)
p=plot(r2,x(2),'b.');
hold on
figure(3)
p=plot(r2,x(3),'g.');
hold on
figure(4)
p=plot(r2,x(4),'c.');
hold on
e(:,r2) = x;
end
function [F] = fun(x,r2)
r3=70.1; %known
r4=70.1; %known
rBE=47.1; %known
rBC=47.1; %known
theta2=0; %known
thetaBE=70; %known
thetaBC=70; %known
F= [r2*cosd(theta2)+r3*cosd(x(1))-r4*cosd(x(2)); % for x: R2 + R3 - R4
r2*sind(theta2)+r3*sind(x(1))-r4*sind(x(2)); % for y: R2 + R3 - R4
rBE*cosd(thetaBE)-x(4)*cosd(x(3))-rBC*cosd(thetaBC); % for x: RBE - R5 - RBC
rBE*sind(thetaBE)-x(4)*sind(x(3))-rBC*sind(thetaBC); % for y: RBE - R5 - RBC
];
end
function [J] = jac(x)
r3=70.1;
r4=70.1;
os3=10;
os4=10;
rBC=47.1;
rBE=47.1;
J=zeros(4,4);
J(1,1)=r3*cosd(x(1));
J(2,1)=-r3*sind(x(1));
J(3,1)=-rBC*cosd(x(1)+os3);
J(4,1)=rBC*sind(x(1)+os3);
J(1,2)=-r4*cosd(x(2));
J(2,2)=r4*sind(x(2));
J(3,2)=rBE*cosd(x(2)+os4);
J(4,2)=-rBE*sind(x(2)+os4);
J(1,3)=0;
J(2,3)=0;
J(3,3)=-x(4)*cosd(x(3));
J(4,3)=x(4)*sind(x(3));
J(1,4)=0;
J(2,4)=0;
J(3,4)=-cosd(x(3));
J(4,4)=sind(x(3));
end
3 Comments
DGM
on 25 Feb 2022
The question shouldn't be "why are my plots empty?", it should be "why am I getting spammed with an infinite number of figure() calls such that I can't even focus the console to kill the script?"
Variable r2 is a constant. The outer loop will never exit.
The inner loop produces this warning:
deltax = j\f;
Warning: Matrix is singular to working precision.
As for the plotting, don't plot things in the loop. In the best case scenario, it'll only slow down the solver. Store all the necessary data and plot after the solver completes.
As far as solving the math goes, you'll have to figure that out.
Caitlynn Sengchiam
on 25 Feb 2022
Arif Hoq
on 25 Feb 2022
I think the problem is in your function fun and jac. whenever your dividing in (deltax(k)=jacfunc(k) / ffunc(k)) the its return an inf. I have found 2 output. but still it needs more development.
%Known Values
r2=140; %input
r3=70.1; %known
r4=70.1; %known
rBE=47.1; %known
r5=140; %unknown
rBC=47.1; %known
theta2=0; %known
theta3=60; %unknown
theta4=60; %unknown
os4=10; %offset from theta4
thetaBE=theta4+os4;%known
theta5=0; %unknown
os3=10; %offset from theta3
thetaBC=theta3+os3; %known
e = zeros(4,360);
error = 1;
x = [theta3;theta4;theta5;r5]; %Matrix of Initial Guesses
tol = 1e-8;
ffunc = fun(x,r2);
jacfunc = jac(x);
deltax = (1:length(x))';
%Position
for r2 = 1:360
i = 0;
while ((error > tol) && (i < 1000))
for k = 1:length(x)
deltax(k)=jacfunc(k) / ffunc(k);
end
xnew = x-deltax;
error = abs((xnew-x)./xnew);
error=max(error);
x=xnew;
i=i+1;
end
e1(:,r2) = x;
end
figure(1)
plot(e1(1,:),'o');
('\r_2 [^{o}]');
hold on
plot(e1(2,:),'*');
('\r_2 [^{o}]');
hold on
plot(e1(3,:),'--');
('\r_2 [^{o}]');
hold on
plot(e1(4,:),'o');
('\r_2 [^{o}]');
hold on
xlabel('r2')
ylabel('outputs')
legend('theta3','theta4','theta5','r5')
Answers (1)
ag
on 4 Jan 2024
0 votes
Hi Caitlynn,
I understand that you are trying to plot the obtained results, and are getting an empty plot.
Arif's suspicion seems to be correct; the issue stems from the calculated values of "jac(x)", which are detailed below.
As you can observe, the cancellation occurring in the first two columns leads to a determinant of zero. Consequently, when applying the formula "deltax(i) = det(jj) / det(j)", you end up with "NaN" since it involves division by zero. Such "NaN" values cannot be plotted.
Note - Jacobian with a zero determinant indicates that there is no net change at that specific point in terms of rate.
Hope this helps!
Best Regards,
Aryan Gupta
Categories
Find more on Programming 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!