How do I store the x's, the v's and the F's from this for loop into an array??? pls and ty

1 view (last 30 days)
Andrew Holland
Andrew Holland on 24 Apr 2015
Edited: Julia on 27 Apr 2015
x=0; %position of the leaf in meters
v=.5; %the velocity of the leaf in meters/seconds
m=0.3; %mass of the leaf in kilograms
t=(0:0.05:3);
h=0.05;
int=0; %the intial time for graphing
%for
for I=(1:60) %Number of instances which is 0 to 3 seconds of steps of .05
%Plot
subplot(1,3,1)
plot(int,x,'.r') %Graph of position over time
hold on
subplot(1,3,2)
plot(int,v,'.r')
hold on
%Equations
F=0.2*(sin(x))*((x^2)+x); %Force equation
a=F/m; %acceleration equation
x=x+h*v;%New Position
v=v+h*a; %New Velocity
subplot(1,3,3)
plot(int,F,'.r')
hold on
int=int+0.05;
end

Answers (1)

Julia
Julia on 24 Apr 2015
Hi,
I just modified your code so that you can store the values for x, v and F.
x=zeros(1,61);
v=zeros(1,61);
F=zeros(1,60);
x(1)=0; %position of the leaf in meters
v(1)=.5; %the velocity of the leaf in meters/seconds
m=0.3; %mass of the leaf in kilograms
t=(0:0.05:3);
h=0.05;
int=0; %the intial time for graphing
%for
for I=(1:60) %Number of instances which is 0 to 3 seconds of steps of .05
%Plot
subplot(1,3,1)
plot(int,x(I),'.r') %Graph of position over time
hold on
subplot(1,3,2)
plot(int,v(I),'.r')
hold on
%Equations
F(I)=0.2*(sin(x(I)))*((x(I)^2)+x(I)); %Force equation
a=F(I)/m; %acceleration equation
x(I+1)=x(I)+h*v(I);%New Position
v(I+1)=v(I)+h*a; %New Velocity
subplot(1,3,3)
plot(int,F(I),'.r')
hold on
int=int+0.05;
end
  4 Comments
Julia
Julia on 27 Apr 2015
Hi,
use an array for int and plot your graphs outside of the loop - for a line graph don't put a '.' in front of the r:
subplot(1,3,1)
plot(int,x,'r') %Graph of position over time
subplot(1,3,2)
plot(int,v,'r')
subplot(1,3,3)
plot(int,F,'r')

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by