Clear Filters
Clear Filters

Why are my plots empty?

4 views (last 30 days)
Caitlynn Sengchiam
Caitlynn Sengchiam on 24 Feb 2022
Answered: ag on 4 Jan 2024
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
Caitlynn Sengchiam
Caitlynn Sengchiam on 25 Feb 2022
I have changed the while loop to a for loop and plotted outside of the loop. My code runs faster, but my plot is still empty.
for r2 = 1: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 = (1:length(x))';
for i = 1:length(x)
jj=j;
jj(:,i)=f;
deltax(i)=det(jj) / det(j);
jj=j;
end
xnew = x-deltax;
error = abs((xnew-x)./xnew);
error=max(error);
x=xnew;
i=i+1;
end
e1(:,r2) = x;
end
r2=(1:360);
figure(1)
plot(r2,e1(1,:),'r.');
('\r_2 [^{o}]');
hold on
plot(r2,e1(2,:),'b.');
('\r_2 [^{o}]');
hold on
plot(r2,e1(3,:),'c.');
('\r_2 [^{o}]');
hold on
plot(r2,e1(4,:),'g.');
('\r_2 [^{o}]');
hold on
xlabel('r2')
ylabel('outputs')
legend('theta3','theta4','theta5','r5')
Arif Hoq
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')

Sign in to comment.

Answers (1)

ag
ag on 4 Jan 2024
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 Graphics Performance in Help Center and File Exchange

Tags

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!