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 Comments

What do you mean by, "I am not able to plot it for..."?
Define all the undefined parameters and update the code.
@Bob Nbob I want to plot 'ye' for every value of the interval 0:0:01:10. I was able to do it for 0:1:10(from code above) but not for 0:0.01:10. @madhan ravi parameters are not the problem.
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.

Sign in to comment.

Answers (2)

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 Comments

If it’s not upto the expectation please let me know.
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'
Your code gives the error, Subscript indices must either be real positive integers or logicals.
The first one or the second one, if it’s the first one say the values for variables which isn’t defined yet?
It's the first one and the values are,
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));
Check the code now.
Please accept the answer if the code is working if not describe what’s needed .

Sign in to comment.

Stephen23
Stephen23 on 30 Aug 2018
Edited: Stephen23 on 30 Aug 2018
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:

Products

Release

R2016a

Tags

Asked:

on 29 Aug 2018

Edited:

on 30 Aug 2018

Community Treasure Hunt

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

Start Hunting!