Plot using array for different plot lines?

조회 수: 19 (최근 30일)
Emiliano Jordan
Emiliano Jordan 2016년 9월 26일
댓글: Emiliano Jordan 2016년 9월 27일
I'm trying to figure out how to make a reusable formula that will plot different lines based on a value set stored in an array. Right now I'm manually running this. The last portion of the equation has a ratio that I'd like to change and add a new plot for each value.
x = 0:1:90; %plot theta 0-90 in 1 degree increments
V = 5; % Use 5 for V since that's what's given in p.75
D = .1; % Use .1m for D since that's what's given in p.75
% Plot 0 d/D ratio. No, hole.
y0 = 1000 * V ^ 2 * (pi / 4) * D ^2 * (1 + sind(x)) * (1 - (0) ^ 2);
% Plot .25 d/D ratio. 25mm hole.
y1 = 1000 * V ^ 2 * (pi / 4) * D ^2 * (1 + sind(x)) * (1 - (0.25) ^ 2);
% Plot .5 d/D ratio. 50mm hole.
y2 = 1000 * V ^ 2 * (pi / 4) * D ^2 * (1 + sind(x)) * (1 - (0.5) ^ 2);
% Plot .75 d/D ratio. 75mm hole.
y3 = 1000 * V ^ 2 * (pi / 4) * D ^2 * (1 + sind(x)) * (1 - (0.75) ^ 2);
% Plot 1 d/D ratio. 100mm hole.
y4 = 1000 * V ^ 2 * (pi / 4) * D ^2 * (1 + sind(x)) * (1 - (1) ^ 2);
plot(x,y0,x,y1,x,y2,x,y3,x,y4);
So, I'm looking for something like this.
x = 0:1:90; %plot theta 0-90 in 1 degree increments
V = 5; % Use 5 for V since that's what's given in p.75
D = .1; % Use .1m for D since that's what's given in p.75
r = 0:.25:1
y = 1000 * V ^ 2 * (pi / 4) * D ^2 * (1 + sind(x)) * (1 - (r) ^ 2);
Then have MATLAB plot the 5 different values of the r array as separate plot lines. I'm new to MATLAB but not to programming. Any literature or a point in the right direction would be great. Thanks!

채택된 답변

David Goodmanson
David Goodmanson 2016년 9월 27일
A vectorized Matlab technique is to use meshgrid:
[xx rr] = meshgrid(x,r)
this makes two matrices each of size { length(r) x length(x) }, the first with the x vector repeated in rows and the second with the r vector repeated in columns as you can check. Then you can create the matrix y
y = 1000*V^2*(pi/4)*D ^2*(1+sind(xx)).*(1 - rr.^ 2);
Note that you need .* and .^, where the periods indicate that the calculation is done for the matrix entries term-by-term, as opposed to some kind of matrix multiplication. You can plot the y matrix with
plot(x,y)
Matlab plots the matrix according to which dimension matches up with x, so in this case you plot out the rows of y with r as the parameter.

추가 답변 (1개)

KSSV
KSSV 2016년 9월 27일
편집: KSSV 2016년 9월 27일
You have to make y a matrix.
x = 0:1:90; %plot theta 0-90 in 1 degree increments
V = 5; % Use 5 for V since that's what's given in p.75
D = .1; % Use .1m for D since that's what's given in p.75
r = 0:.25:1 ;
y = zeros(length(r),length(x)) ; % initialization
for i = 1:length(r)
y(i,:) = 1000*V^2*(pi/4)*D ^2*(1+sind(x))*(1 - r(i)^ 2);
end
plot(x,y) ;
  댓글 수: 1
Emiliano Jordan
Emiliano Jordan 2016년 9월 27일
Thanks! That's exactly what I was looking for.

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

카테고리

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

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by