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
  2 件のコメント
Takura Nyatsuro
Takura Nyatsuro 2022 年 11 月 15 日
That makes sense , thanks for the help :)
Les Beckham
Les Beckham 2022 年 11 月 15 日
You are quite welcome.

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by