Hi, I'm not the most experienced Matlab user, and I'm trying to plot 2 functions as a function of a variable from 0 to 360 degrees. When I type in a value for t2. Apo4x and Apo4y return values. The problem is when I set t2 to a range of values, Apo4x and Apo4y return 0 for every iteration.
Below is my code.
a=1; b=4; c=8; d=1.3; k=0.3; g=5;
t2dot=10;
t2ddot=0;
t2=(pi/180)*[0:5:360]
t4=atan2(a*sin(t2)+k, a*cos(t2)+d);
f=(a*cos(t2)+d)/cos(t4);
A=[f*sin(t4),-cos(t4);-f*cos(t4),-sin(t4)];
B=[t2dot*a*sin(t2);-t2dot*a*cos(t2)];
C=A\B;
t4dot=C(1);
fdot=C(2);
Q1=-t2dot*a*sin(t2)-t4dot*b*sin(t4)-fdot*cos(t4)+t4dot*f*sin(t4);
Q2=t2dot*a*cos(t2)+t4dot*b*cos(t4)-fdot*sin(t4)-t4dot*f*cos(t4);
t5=asin((g-b*sin(t4))/c);
D=[c*sin(t5),-1;-c*cos(t5),0];
E=[Q1;Q2];
F=D\E;
t5dot=F(1);
hdot=F(2);
Q3=-t2ddot*a*sin(t2)-t2dot^2*a*cos(t2)+t4dot^2*f*cos(t4)+t4dot*fdot*sin(t4);
Q4=t2ddot*a*cos(t2)-t2dot^2*a*sin(t2)+t4dot^2*f*sin(t4)-t4dot*fdot*cos(t4);
G=[-f*sin(t4),-t4dot*sin(t4)+cos(t4);f*cos(t4),t4dot*cos(t4)+sin(t4)];
H=[Q3;Q4];
J=G\H;
t4ddot=J(1);
fddot=J(2);
Q5=-t4ddot*b*sin(t4)-t4dot^2*b*cos(t4)-t5dot^2*c*cos(t5);
Q6=t4ddot*b*cos(t4)-t4dot^2*b*sin(t4)-t5dot^2*c*sin(t5);
K=[c*sin(t5),-1;-c*cos(t5),0];
L=[Q5;Q6];
M=K\L;
t5ddot=M(1);
hddot=M(2);
Apo4x=-t4ddot*b*sin(t4)-t4dot^2*b*cos(t4)-t5ddot*(c/2)*sin(t5)-t5dot^2*(c/2)*cos(t5);
Apo4y=t4ddot*b*cos(t4)-t4dot^2*b*sin(t4)+t5ddot*(c/2)*cos(t5)-t5dot^2*(c/2)*sin(t5);
plot(t2,Apo4x,'-r',t2,Apo4y,'-b')
and here is a similar code I wrote that didn't have said problem. Why does the bottom code work while the top doesn't? How can I fix this? Thanks for your time!
format compact
a=20;
b=55;
c=8;
p=30;
t2=(pi/180)*[0:5:360]
t2dot=10;
t3=asin((a*sin(t2)-c)/b);
t3dot=-t2dot*(a*cos(t2))/(b*cos(t3));
ddot=-t2dot*a*sin(t2)+t3dot*b*sin(t3);
Vpx=-t2dot*a*sin(t2)+t3dot*(b*sin(t3)-28.8*sin(t3+20.95*pi/180))
Vpy=t2dot*a*cos(t2)-t3dot*(b*cos(t3)+28.7*cos(t3+20.95*pi/180));
plot(t2,Vpx,'-r',t2,Vpy,'-b')

 채택된 답변

Star Strider
Star Strider 2015년 3월 30일

1 개 추천

All the ‘t#dot’ values are zero. I gather that you don’t want them to be, so figure out that problem first. That most likely requires that you examine the matrix calculations that create them.

댓글 수: 4

Tanner
Tanner 2015년 3월 30일
Yeah I see that, thanks for the reply!
However the 't#dot' values return a number when t2 is assigned a single value. They only return 0 when I assign t2 from 0 to 360.
I'll look into the matrix calculations, though. Am I assigning t2 to a range of individual iterative values or a matrix of values? If it's the latter I imagine that would be my problem.
Star Strider
Star Strider 2015년 3월 30일
How do you want to calculate the ‘t#dot’ values? (I have problems mapping from your lower code that works with scalar values to your non-function code.) For instance, ‘A’ is (2x146) and ‘B’ is (2x73). This will create unequal dimensions problems if you want to do element-wise operations, and the backslant (\) does a least-squares approximation of the equivalent of C=inv(A)*B, making ‘C’ (146x73).
Tanner
Tanner 2015년 3월 30일
I think I see what you're saying. A should be (2x4), B should be (2x2), C should be (2x1) the entire time through. I want crunch all the numbers for one value of t2. plot one value of Apo4x and one value of Apo4y, and then repeat for the next t2 value.
I tried to do a for loop but with no luck. User error I'm sure. Instead of 't2=(pi/180)*[0:5:360]' I used 'for t2=0:.1:2*pi' and 'end' was after everything including the plot command.
It plotted a single point. I'm not sure the correct way to go about it.
If you added (or intend to add) a loop, be sure that you (1) index the ‘t#dot’ values and their friends with the loop index to create an array of them, and (2) put the plot call after the loop.
The loop structure I would use (albeit not understanding what you’re doing), would be something like:
t2=(pi/180)*[0:5:360];
for k1 = 1:length(t2)
t4(k1)=atan2(a*sin(t2(k1))+k, a*cos(t2(k1))+d);
f=(a*cos(t2(k1))+d)/cos(t4(k1));
... CODE [WITH (k1) INDICES EVERYWHERE THEY’RE NEEDED] ...
end
figure(1)
plot(t2,Apo4x,'-r',t2,Apo4y,'-b')
Even with the ‘Search and Replace’ capabilities of the Editor, that’s going to be really tedious, but in the end, likely worthwhile. (Replace ‘t2’ with ‘t2(k1)’ to make this easier.) If you don’t need to keep an array of variables (for instance, ‘f’), you don’t need to subscript them. (Note: I use ‘k1’ for my outer loop counter, ‘k2’ for the first inner loop counter, etc. Just my way of keeping track of them. I don’t see that you’ve used those anywhere in your code, so it’s likely safe as written.)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

태그

질문:

2015년 3월 30일

댓글:

2015년 3월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by