MATLAB Answers

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 ビュー (過去 30 日間)
Andrew Holland
Andrew Holland 2015 年 4 月 24 日
編集済み: Julia 2015 年 4 月 27 日
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

回答 (1 件)

Julia
Julia 2015 年 4 月 24 日
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 件のコメント
Julia
Julia 2015 年 4 月 27 日
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')

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

Community Treasure Hunt

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

Start Hunting!

Translated by