Filter löschen
Filter löschen

Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

Error with plotting function, help please

1 Ansicht (letzte 30 Tage)
Michael
Michael am 30 Mai 2013
Geschlossen: MATLAB Answer Bot am 20 Aug. 2021
So in my mind this script should graph a camber vs height graph for 3 separate 'a' link lengths, for some reason it's coming up with an error and not graphing anything. It works if you remove the for loop for the 'a' link and replace it with a single value, so i'm not sure what's going wrong with it, i'm pretty new to matlab so any help would be appreciated. Here's the code
%Original A-Link
%
% Lengths of Linkages
a=[200:50:300];
for j=200:3;
a(j)=j
b=182.014;
c=345.91;
d=154.91;
e=110.524;
% Initial Angles
theta0=0.544892;
theta20=2.504722;
theta30=0.612262;
alpha=0.559028959;
% J-Values & Geometry Calculations
j1=d/a;
j2=d/c;
j3=(a^2 - b^2 + c^2 + d^2)/(2*a*c);
j4=d/b;
j5=(c^2 - d^2 - a^2 - b^2)/(2*a*b);
q=atan((a*sin(theta20))/(d-(a*cos(theta20))));
f=sqrt(a^2 +d^2 - 2*a*d*cos(theta20));
p=atan((sqrt(4*b^2*f^2 - (b^2 + f^2 - c^2)^2))/(b^2 +f^2 - c^2));
% Coupler Initial Position
xc=a*cos(theta20) + e*cos(p-q+alpha);
yc=a*sin(theta20) + e*sin(p-q+alpha);
z=-xc*cos(theta0) - yc*sin(theta0);
% Range Of Input Angles
theta2=[(theta20-(degtorad(45))):pi/180:(theta20+(degtorad(45)))];
% Array
camber=[]; height=[];
% Loop
for i=theta2;
D=j5-j1+((1+j4)*cos(i));
E=-2*sin(i);
F=j5+j1-((1-j4)*cos(i));
theta3=2*atan((-E-sqrt(E^2 - 4*D*F))/(2*D));
% Camber Angle
camber(end+1)=radtodeg(theta30-theta3);
q1=atan((a*sin(i))/(d-(a*cos(i))));
f1=sqrt(a^2 +d^2 - 2*a*d*cos(i));
p1=atan((sqrt(4*b^2*f1^2 - (b^2 + f1^2 - c^2)^2))/(b^2 +f1^2 - c^2));
% Coupler Position
xc1=a*cos(i) + e*cos(p-q+alpha);
yc1=a*sin(i) + e*sin(p-q+alpha);
z1=-xc1*cos(theta0) - yc1*sin(theta0);
height(end+1)=z1-z;
end
end
hold on
% Plot Results
A=plot(camber,height);
set(A,'-','linewidth',1)
xlabel('camber angle(degrees)','FontSize',18)
ylabel('Wheel Travel(mm)','FontSize',18)
title('Camber Angle With Wheel Travel','FontSize',20)
cheers michael

Antworten (3)

David Sanchez
David Sanchez am 30 Mai 2013
first things first. Your for loop should be written with a _(-1)_decrement in order to work. Instead of
for j=200:3
write it like
for j=200:-1:3
You define a=[200:50:300], but later assign a(200) = 200 !!! Try to initialize your arrays properly.
In the assignment
j1=d/a;
You should use the dot notation since a is an array:
j1=d./a;
Correct your code, think carefully about what you are doing and what you want to do

David Sanchez
David Sanchez am 30 Mai 2013
It this the code you need/want? It works now, but since it is your code, I don't know whether it is what you really want.
a=[200:50:300];
for j=200:-1:3;
a=j;
b=182.014;
c=345.91;
d=154.91;
e=110.524;
% Initial Angles
theta0=0.544892;
theta20=2.504722;
theta30=0.612262;
alpha=0.559028959;
% J-Values & Geometry Calculations
j1=d/a;
j2=d/c;
j3=(a^2 - b^2 + c^2 + d^2)/(2*a*c);
j4=d/b;
j5=(c^2 - d^2 - a^2 - b^2)/(2*a*b);
q=atan((a*sin(theta20))/(d-(a*cos(theta20))));
f=sqrt(a^2 +d^2 - 2*a*d*cos(theta20));
p=atan((sqrt(4*b^2*f^2 - (b^2 + f^2 - c^2)^2))/(b^2 +f^2 - c^2));
% Coupler Initial Position
xc=a*cos(theta20) + e*cos(p-q+alpha);
yc=a*sin(theta20) + e*sin(p-q+alpha);
z=-xc*cos(theta0) - yc*sin(theta0);
% Range Of Input Angles
theta2=[(theta20-(degtorad(45))):pi/180:(theta20+(degtorad(45)))];
% Array
camber=[]; height=[];
% Loop
for i=theta2
D=j5-j1+((1+j4)*cos(i));
E=-2*sin(i);
F=j5+j1-((1-j4)*cos(i));
theta3=2*atan((-E-sqrt(E^2 - 4*D*F))/(2*D));
% Camber Angle
camber(end+1)=radtodeg(theta30-theta3);
q1=atan((a*sin(i))/(d-(a*cos(i))));
f1=sqrt(a^2 +d^2 - 2*a*d*cos(i));
p1=atan((sqrt(4*b^2*f1^2 - (b^2 + f1^2 - c^2)^2))/(b^2 +f1^2 - c^2));
% Coupler Position
xc1=a*cos(i) + e*cos(p-q+alpha);
yc1=a*sin(i) + e*sin(p-q+alpha);
z1=-xc1*cos(theta0) - yc1*sin(theta0);
height(end+1)=z1-z;
end
end
hold on
% Plot Results
A=plot(camber,height,'linewidth',1);
xlabel('camber angle(degrees)','FontSize',18)
ylabel('Wheel Travel(mm)','FontSize',18)
title('Camber Angle With Wheel Travel','FontSize',20)

Michael
Michael am 30 Mai 2013
Thanks for the help, i managed to get it working on my own. i made 4 different loops and plotted all 4 of them on the same graph. it works but it's a pretty long code for something that in my mind should be much simpler. like i said i'm new to matlab.
thanks again!

Diese Frage ist geschlossen.

Community Treasure Hunt

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

Start Hunting!

Translated by