Hi! I am working on a code where I have 3 functions that overtake from each other at t=0 to 5, 5 to 15.4 and 15.4 to 25.
later I am trying to integrate them using the trapezoidal rule, but before I tried to combine 3 functions with poly2sum function. But my integral appears as x10^24 which is definitely not correct. Does anyone know how can I use trapezoidal rule to intregrate these 3 functions when t= as described above or how to combine them into 1 function?
Please need help
clc,clear,close all
f=[];
for t=1:25
if t<5
f(t)=0.1553567*(t.^6) - 2.0416*(t.^5) + 9.1837*(t.^4) - 14.829*(t.^3) - 1.3703*(t.^2) + 32.821*(t) - 1.3155;
elseif (t>=5)&&(t<15.4)
f(t)=0.003980879*(t.^5) - 0.2247*(t.^4) + 4.8682*(t.^3) - 50.442*(t.^2) + 254.67*(t) - 430.66;
elseif t>=15.4
f(t)=-0.073*(t.^2) + 6.1802*(t) + 40.423;
end
end
syms t
plot(f)
xlabel('Time(s)')
ylabel('Speed(mph)')
grid on
title('Speed(mph) vs Time(s)')
f1=f;
c=poly2sym(f,t);
f=@(t) (3181181871604081*t.^24)/140737488355328 + (2235201853553569*t.^23)/70368744177664 + (799700918963229*t.^22)/17592186044416 + (3923588293415859*t.^21)/70368744177664 + (1093666568987161*t.^20)/17592186044416 + (1279362089076253*t.^19)/17592186044416 + (1364564885566369*t.^18)/17592186044416 + (1425048751384133*t.^17)/17592186044416 + (1500543524306005*t.^16)/17592186044416 + (1603138970636933*t.^15)/17592186044416 + (1725688668888861*t.^14)/17592186044416 + (1850213893459389*t.^13)/17592186044416 + (1956307498310429*t.^12)/17592186044416 + (2029537800646613*t.^11)/17592186044416 + (2069852464594085*t.^10)/17592186044416 + (1060968907371289*t.^9)/8796093022208 + (8753125867013119*t.^8)/70368744177664 + (4504113319202993*t.^7)/35184372088832 + (2313263393287229*t.^6)/17592186044416 + (592975416952619*t.^5)/4398046511104 + (2427971483171239*t.^4)/17592186044416 + (2481472839369517*t.^3)/17592186044416 + (1266202868202655*t.^2)/8796093022208 + (5161540348557237.*t)/35184372088832 + 1313283076494721/8796093022208;
a = 1;
b = 9;
d_exact = integral(f,a,b); % exact distance
%% TRAPEZOIDAL RULE
Fa = f(a);
Fb = f(b); % first and last elements
err = Inf; % intitial absolute error
n = 1; % number of sections
i = 1; % iteration
while err >= 0.00002
h = (b-a)/n;
t = a;
sigma = 0;
for k = 1:n-1
t = t+h;
sigma = sigma + f(t);
end
d_approx = (h/2)*(Fa+ 2*sigma + Fb);
err = abs(d_exact-d_approx)/d_exact;
no_sec(i) = n;
distance(i) = d_approx;
Err_pert(i) = err*100;
i = i + 1;
n = 2*n;
end
fprintf('\nn(sections)\t\tD(m)\tRelative Error(%%)')
for i=1:numel(no_sec)
fprintf('\n\t%d\t\t\t%0.e\t\t%0.3f',no_sec(i),distance(i),Err_pert(i))
end
fprintf('\n\n')
%% b)
t = linspace(a,b,25);
v = f1;
dist = v.*t;
figure;
subplot(2,1,1)
plot(v)
title('Speed vs Time')
xlabel('t(s)')
ylabel('Speed(m/s)')
grid on
subplot(2,1,2)
plot(dist)
title('Distance vs Time')
xlabel('T(s)')
ylabel('Distance(m)')
grid on
%% c)
figure;
semilogx(no_sec,distance)
xlabel('Sections [n]')
ylabel('Distance (m)')
grid on

Réponses (1)

Michael Gareis
Michael Gareis le 9 Nov 2020

0 votes

Try defining your equation with piecewise(). https://www.mathworks.com/help/symbolic/piecewise.html

Catégories

En savoir plus sur Programming dans Centre d'aide et File Exchange

Produits

Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by