Plot using array for different plot lines?
19 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
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.
0 个评论
更多回答(1 个)
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) ;
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Line Plots 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!