How to plot in a for loop using real numbers?
显示 更早的评论
Hi everyone,I am trying to plot a graph of deflection of a beam vs length of the beam, but I am not able to plot it for the interval 0:0.01:10. Any guidance in this regard will be greatly appreciated.
for i= 1:11
x(i)=i- 1;
if x(i) <= L/2
ye(i)= ((x(i).^3/6) - ((3*q*L*L*x(i).^2)/16) + ((q*L*x(i).^3)/12) + ((F*L*x(i).^2)/2))/(E*I);
else
ye(i)= ((-(q*(x(i)-((L/2)^4)))/24) + ((-((F*(x(i).^3))/6)) - (3*(q*L*L*(x(i).^2))/16) + ((q*L*x(i).^3)/12) + ((F*L*x(i).^2)/2)))/(E*I);
end
end
y=0:1:10;
plot(y,ye,'r--')
xlim([0 10])
4 个评论
Bob Thompson
2018-8-29
What do you mean by, "I am not able to plot it for..."?
madhan ravi
2018-8-29
Define all the undefined parameters and update the code.
Bhushan Patil
2018-8-30
Note that the code in the question (and also in madhan ravi's answer) calculates values using the index, not by using the independent variable y. This means the output is basically meaningless, as there is absolutely no connection between the calculated value and the independent variable, so you do NOT have a function of y. Consider these two examples:
ye1 = ... your code
y1 = 0:10; % eleven values
plot(y1,ye1)
ye2 = ... your code
y2 = 23:33; % eleven values
plot(y2,ye2)
will give exactly the same ye values (i.e. ye1==ye2), even though the independent variables y1 and y2 have totally different values. In fact you could even define y to have totally random values, and it makes no difference to your calculation, and this would still give exactly the same ye values. What is the point of that?
Should you (as this question requests) calculate more points over the same range:
ye3 = ... your code (more indices).
y3 = 0:0.1:10 % more points, but end is still 10!
plot(y3,ye3)
then, because ye depends only on the indices and not the values of y, the last value of ye3 will not be the same as ye1, although they should be the as they are apparently being plotted against the same y value: y(end)=10 for both vectors... then why is ye(end) different?
You actually need to calculate your ye values using the values of y. Indices are not data values.
回答(2 个)
madhan ravi
2018-8-29
编辑:madhan ravi
2018-8-30
q = 4000;
L = 10;
E = 200*(10^9);
I = 363.76 * (10^-8);
K = 3000;
F = ((41*q*(L^4))/384)/(((E*I)/K)+(L^3/3));
y=0:0.01:10
for i= 1:length(y) %to plot with same length
x(i)=i- 1;
if x(i) <= L/2
ye(i)= ((x(i).^3/6) - ((3*q*L*L*x(i).^2)/16) + ((q*L*x(i).^3)/12) + ((F*L*x(i).^2)/2))/(E*I);
else
ye(i)= ((-(q*(x(i)-((L/2)^4)))/24) + ((-((F*(x(i).^3))/6)) - (3*(q*L*L*(x(i).^2))/16) + ((q*L*x(i).^3)/12) + ((F*L*x(i).^2)/2)))/(E*I);
end
end
plot(y,ye,'r--')
xlim([0 10])
7 个评论
madhan ravi
2018-8-29
If it’s not upto the expectation please let me know.
madhan ravi
2018-8-29
Here is an example of plotting inside a loop:
clc
close all
clear all
x=linspace(-10,10,1000);
'on'
for i=1:length(x);
y=(10.*sin(x).^2)./4;
% xx= linspace(0,10,1000);
% YY=spline(nn, y,xx);
% plot( nn, y(1,:),'o',xx,YY(1,:),'-')
a=plot(x(1:i),y(1:i),'-');
a.Color='m';
xlim([-10 10])
ylim([-10 10])
pause(0.01);
hold on ;
end
'off'
Bhushan Patil
2018-8-30
madhan ravi
2018-8-30
The first one or the second one, if it’s the first one say the values for variables which isn’t defined yet?
Bhushan Patil
2018-8-30
madhan ravi
2018-8-30
Check the code now.
madhan ravi
2018-8-30
Please accept the answer if the code is working if not describe what’s needed .
You probably want to use indexing, something like this:
q = 4000;
L = 10;
E = 200*(10^9);
I = 363.76 * (10^-8);
K = 3000;
F = ((41*q*(L^4))/384)/(((E*I)/K)+(L^3/3));
x = 0:1:10;
y = nan(size(x));
idx = x<(L/2);
y(idx) = ((x(idx).^3/6) - (3*q*L*L*x(idx).^2)/16 + (q*L*x(idx).^3)/12 + (F*L*x(idx).^2)/2)/(E*I);
idx = ~idx;
y(idx)= (-(q*(x(idx)-(L/2)^4))/24 + (-(F*(x(idx).^3))/6 - 3*(q*L*L*(x(idx).^2))/16 + (q*L*x(idx).^3)/12 + (F*L*x(idx).^2)/2))/(E*I);
plot(x,y,'r--')
Which generates this plot:

类别
在 帮助中心 和 File Exchange 中查找有关 Code Performance 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!