Cumulative distance vs time graph using a velocity vs time graph

조회 수: 4 (최근 30일)
Takura Nyatsuro
Takura Nyatsuro 2022년 11월 14일
댓글: Les Beckham 2022년 11월 15일
Hi, i have been given a formula for a velocity and a time interval and i have been tasked with producing a velocity vs time graph , a cumulative distance vs time graph as well as total distance travelled. I have currently got the velocity vs time graph working and im trying to plot a cumulative distance vs time graph by means of intergration, but i can only manage to get the total distance. I am unsure on how to go about this problem. any help would be appreciated.
Thank you
clc;
clear;
close;
hold on;
grid on;
t=0:60;
a=0;
b=60;
n=200;
area=zeros(1,60);
speed=-0.073*(t.^2)+6.1802*(t);
plot(t,speed);
dx=(b-a)/n;
x=a:dx:b;
H=zeros(1,n);
for i=1:n
fxmiddle=-0.073*((x(i)^2+x(i+1)^2))/2+6.1803*((x(i)+x(i+1))/2);
fxLeft=-0.073*(x(i)^2)+6.1802*(x(i));
fxright=-0.073*(x(i+1)^2)+6.1802*(x(i+1));
H(i)=(fxLeft+4*fxmiddle+fxright)/6;
end
%simpsons rule
integ=0;
for i=1:n
A=H(i)*dx;
integ=integ+A;
end
plot(t,integ);
syms x f(x)
f(x)=-0.073*(x^2)+6.1802*(x);
truearea=int(f(x),x,0,60);
relative_err_frac=((integ-truearea)/truearea)*100;
relative_err=double(relative_err_frac);
if relative_err>0.06
print('ERROR, RELATIVE ERROR TOO HIGH, PLEASE RE INPUT VALUES');
end

채택된 답변

Les Beckham
Les Beckham 2022년 11월 14일
편집: Les Beckham 2022년 11월 15일
You need to save the value of integ on each step of the integration.
See changes below marked with % <<<
clc;
clear;
close;
t=0:60;
a=0;
b=60;
n=200;
area=zeros(1,60);
speed=-0.073*(t.^2)+6.1802*(t);
plot(t,speed);
grid on;
dx=(b-a)/n;
x=linspace(a, b, n); % <<< make x the same size and H (and thus integ)
H=zeros(1,n);
for i=1:n-1 % <<<
fxmiddle=-0.073*((x(i)^2+x(i+1)^2))/2+6.1803*((x(i)+x(i+1))/2);
fxLeft=-0.073*(x(i)^2)+6.1802*(x(i));
fxright=-0.073*(x(i+1)^2)+6.1802*(x(i+1));
H(i)=(fxLeft+4*fxmiddle+fxright)/6;
end
%simpsons rule
integ=zeros(size(H)); % <<< Preallocate integ
for i=2:n
A=H(i)*dx;
integ(i)=integ(i-1) + A; % <<< Save each sample of the integration
end
plot(x,integ); % <<< use x here instead of t (I'm not sure why you just didn't make b and n the same)
grid on
syms x f(x)
f(x)=-0.073*(x^2)+6.1802*(x);
truearea=int(f(x),x,0,60);
relative_err_frac=((integ-truearea)/truearea)*100;
relative_err=double(relative_err_frac);
if relative_err>0.06
print('ERROR, RELATIVE ERROR TOO HIGH, PLEASE RE INPUT VALUES');
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by