How to plot function with multiple outputs on same graph
11 views (last 30 days)
Show older comments
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!
0 Comments
Answers (3)
dpb
on 25 Sep 2015
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.
0 Comments
Kevin Doherty
on 25 Sep 2015
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.
0 Comments
andrew henken
on 25 Sep 2015
2 Comments
dpb
on 26 Sep 2015
Edited: dpb
on 26 Sep 2015
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
on 2 Oct 2015
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
See Also
Categories
Find more on Annotations 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!