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

조회 수: 3 (최근 30일)
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 CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by