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

2 ビュー (過去 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 件のコメント
Andrew Holland
Andrew Holland 2015 年 4 月 24 日
This is what I currently have
%Problem4
%Clear Everything
clear,clc,clf
x=zeros(1,61);
v=zeros(1,61);
F=zeros(1,61);
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.0525; % to make the x axis look nice
end
%Print
fprintf('The location of the particle after 3 seconds is %0.4f\n',x(end))
%Labels
subplot(1,3,1)
title ('Position vs Time')
xlabel ('Time (s)')
ylabel ('Position (m)')
subplot(1,3,2)
title ('Velocity vs Time')
xlabel ('Time (s)')
ylabel ('Velocity (m/s)')
subplot(1,3,3)
title ('Force vs Time')
xlabel ('Time (s)')
ylabel ('Force (N)')
Julia
Julia 2015 年 4 月 27 日
編集済み: 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')

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

カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by