How to plot function with multiple outputs on same graph

조회 수: 8 (최근 30일)
andrew henken
andrew henken 2015년 9월 25일
댓글: Kevin Doherty 2015년 10월 2일
I have a function that has one input and gives back 4 outputs. i would like to plot theta3open vs theta2 and theta4open vs theta2 on the same graph then plot Py vs Px on another graph. This is my second day with matlab so I dont really know what I'm doing. Here is the function I'm using.
function [Px,Py,theta3open,theta4open] = fourbarcalc(theta2)
a = 1.0;
b=2.06;
c=2.33;
d=2.22;
K1 = d/a; K2 = d/c;
K3 = (a^2 - b^2 + c^2 + d^2)/(2*a*c);
%%2. Use these link ratio to find the
% intermediate parameters A, B, and C from
% equation 4.10a
A = cosd(theta2) - K1 - K2*cosd(theta2) + K3;
B = -2*sind(theta2);
C = K1 - (K2+1)*cosd(theta2) + K3;
%%3. Use equation 4.10b to find theta4
theta4open = 2*atan2d(-B-sqrt(B^2-4*A*C),2*A)+360
%%4. Use equation 4.11b to find the ratios
% K4 and K5
K4 = d/b;
K5 = (c^2 - d^2 - a^2 - b^2)/(2*a*b);
%%5. Use equation 4.12 to find D, E, F
D = cosd(theta2) - K1 + K4*cosd(theta2) + K5;
E = -2*sind(theta2);
F = K1 + (K4-1)*cosd(theta2) + K5;
%%6. Use equation 4.13 to find theta3
theta3open = 2*atan2d(-E-sqrt(E^2-4*D*F),2*D)+360
Px=a*cosd(theta2)+3.06*cosd(theta3open-31)
Py=a*sind(theta2)+3.06*sind(theta3open-31)
end
I tried using fplot but it only plots the first output. Any help is greatly appreciated!

답변 (3개)

dpb
dpb 2015년 9월 25일
figure
plot(theta2(:),[theta3open(:),theta4open(:)])
figure
plot(px,py)
NB: The (:) ensures the vectors are column-oriented. plot (and Matlab in general being column-major storage order) treats each column in a 2D array as a separate variable; hence the necessity of ensuring that orientation.

Kevin Doherty
Kevin Doherty 2015년 9월 25일
figure
plot(theta2,theta3open,theta2,theta4open)
figure
plot(Px,Py)
I think something like that is what you're looking for. fplot is for plotting functions, which is not what you're trying to do.

andrew henken
andrew henken 2015년 9월 25일
I dont think I explained that very well. I'm looking for a plot of theta3open as a function of theta2 etc...so that I have a curve, not just a single point. Basically if I for loop the function from theta2=0 to theta2=360 I want two graphs. Or am I doing it wrong still?
  댓글 수: 2
dpb
dpb 2015년 9월 26일
편집: dpb 2015년 9월 26일
Use comments instead of Answer box to add comments (so to speak :) ) and clarifications...
That aside, "yes, you are still doing it wrong". :)
Call the function as
theta2=[0:360].';
[Px,Py,theta3open,theta4open] = fourbarcalc(theta2)
and you'll like magic get back the column arrays computed for the range. It's "how Matlab works" and is the reason for "MATrixLABoratory" in the name.
If that isn't clear, go to the "Getting Started" section in the doc and work thru the introductory exercises on addressing, array manipulation, etc., ...
Kevin Doherty
Kevin Doherty 2015년 10월 2일
Yes, I realise you're plotting theta3open as a function of theta2. But when I said that fplot is for functions, I meant Matlab functions. Although in your problem you are thinking of theta3open as a function of theta2 you have not defined it as a function in Matlab. What you want to do here is plot the values in the vector theta3open against the values in the vector theta2. This is what plot is for. http://uk.mathworks.com/help/matlab/ref/function.html

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

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by