Plot using array for different plot lines?

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 日

1 投票

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 日

1 投票

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.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLine Plots についてさらに検索

製品

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by