Cumulative distance vs time graph using a velocity vs time graph
27 views (last 30 days)
Show older comments
Takura Nyatsuro
on 14 Nov 2022
Commented: Les Beckham
on 15 Nov 2022
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
0 Comments
Accepted Answer
Les Beckham
on 14 Nov 2022
Edited: Les Beckham
on 15 Nov 2022
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 Comments
More Answers (0)
See Also
Categories
Find more on Calculus in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!