How to define the function to use for the composite trapezoidal rule?

4 visualizzazioni (ultimi 30 giorni)
Im trying to define the function to be integrated possibly in a sub-function, but I cant seem to do it without it saying INVALID.
My work is below.
Thanks
clc
clear
%intial conditions
t=linspace(1,10,10);
v=zeros(size(t));
m=1609.344/3600;%conversion from mph to m/s
for i=1:numel(t)
v(i)=((0.0011*(t(i)^6))-(0.02928*(t(i)^05))+(0.2807*(t(i)^4))-(1.1837*(t(i)^3))-(0.8283*(t(i)^2))+(41.234*(t(i))-3.3549))*m;
hold on
end
plot(t,v,'r*-')
title('9 Seconds of Car Driving-Speed(m/s)vs.Time(s)')
xlabel('Time(s)')
ylabel('Speed(m/S)')
a=1; %lower limit
b=10; %upper limit
n=9; %number of sub-intervals
r_error=0.1;%intial realtive error
i=0; %loop counter
I=0; %integral set to zero
%while loop to find new integral with a realtive error less than 0.0002%
while r_error>0.00002
h=(b-a)/n; %distnace of each separation
new_i=0; %intially set to zero
i=i+1; %increase counter by 1 every loop
for m=1:n
x_left=a+(i-1)*h;
x_right=a+(i*h);
f_left=f(x_left);
f_right=f(x_right);
new_i=(h/2)*(f_left+f_right)+I; %new integral
New_x=new_i; %new distance is equal to new integral
midpoint=(a+b)/2;
end
r_error=((new_i-I)/new_i)*100; %realtive error
n1(i)=n;
New_x=new_i;
n=n*2; %number of separtions doubled every loop
I=new_i; %new integral is stores as old integral for new loop
fprintf('Integral=%6.3f\n',I) %new integral value displayed to 3 decimal places
fprintf('Number of separations=%6.0f\n',n) % displaying number of separations
fprintf('Relative Error=%6.5f\n',r_error) %displaying relative error to 5 decimal places
end
function v=f(t)
v=((0.0011*t^6)-(0.02928*t^5)+(0.2807*t^4)-(1.1837*t^3)-(0.8283*t^2)+(41.243*t)-3.3549);
end

Risposta accettata

KALYAN ACHARJYA
KALYAN ACHARJYA il 24 Lug 2019
Modificato: KALYAN ACHARJYA il 24 Lug 2019
"but I cant seem to do it without it saying INVALID."
I did not find any coding error
Main Script:
%intial conditions
t=linspace(1,10,10);
v=zeros(size(t));
m=1609.344/3600;%conversion from mph to m/s
for i=1:numel(t)
v(i)=((0.0011*(t(i)^6))-(0.02928*(t(i)^05))+(0.2807*(t(i)^4))-(1.1837*(t(i)^3))-(0.8283*(t(i)^2))+(41.234*(t(i))-3.3549))*m;
hold on
end
plot(t,v,'r*-')
title('9 Seconds of Car Driving-Speed(m/s)vs.Time(s)')
xlabel('Time(s)')
ylabel('Speed(m/S)')
a=1; %lower limit
b=10; %upper limit
n=9; %number of sub-intervals
r_error=0.1;%intial realtive error
i=0; %loop counter
I=0; %integral set to zero
%while loop to find new integral with a realtive error less than 0.0002%
while r_error>0.00002
h=(b-a)/n; %distnace of each separation
new_i=0; %intially set to zero
i=i+1; %increase counter by 1 every loop
for m=1:n
x_left=a+(i-1)*h;
x_right=a+(i*h);
f_left=f(x_left);
f_right=f(x_right);
new_i=(h/2)*(f_left+f_right)+I; %new integral
New_x=new_i; %new distance is equal to new integral
midpoint=(a+b)/2;
end
r_error=((new_i-I)/new_i)*100; %realtive error
n1(i)=n;
New_x=new_i;
n=n*2; %number of separtions doubled every loop
I=new_i; %new integral is stores as old integral for new loop
fprintf('Integral=%6.3f\n',I) %new integral value displayed to 3 decimal places
fprintf('Number of separations=%6.0f\n',n) % displaying number of separations
fprintf('Relative Error=%6.5f\n',r_error) %displaying relative error to 5 decimal places
end
Function f (save in same directory)
function v=f(t)
v=((0.0011*t^6)-(0.02928*t^5)+(0.2807*t^4)-(1.1837*t^3)-(0.8283*t^2)+(41.243*t)-3.3549);
end
Results:
Integral=53.051
Number of separations= 18
Relative Error=100.00000
Integral=84.010
Number of separations= 36
Relative Error=36.85149
Integral=98.508
Number of separations= 72
Relative Error=14.71783
Integral=104.973
Number of separations= 144
Relative Error=6.15875
Integral=107.866
Number of separations= 288
Relative Error=2.68245
Integral=109.191
Number of separations= 576
Relative Error=1.21350
Integral=109.814
Number of separations= 1152
Relative Error=0.56711
Integral=110.113
Number of separations= 2304
Relative Error=0.27165
Integral=110.259
Number of separations= 4608
Relative Error=0.13234
Integral=110.331
Number of separations= 9216
Relative Error=0.06517
Integral=110.367
Number of separations= 18432
Relative Error=0.03230
Integral=110.384
Number of separations= 36864
Relative Error=0.01607
Integral=110.393
Number of separations= 73728
Relative Error=0.00802
Integral=110.398
Number of separations=147456
Relative Error=0.00400
Integral=110.400
Number of separations=294912
Relative Error=0.00200
Integral=110.401
Number of separations=589824
Relative Error=0.00100
Integral=110.401
Number of separations=1179648
Relative Error=0.00050
Integral=110.402
Number of separations=2359296
Relative Error=0.00025
Integral=110.402
Number of separations=4718592
Relative Error=0.00012
Integral=110.402
Number of separations=9437184
Relative Error=0.00006
Integral=110.402
Number of separations=18874368
Relative Error=0.00003
Integral=110.402
Number of separations=37748736
Relative Error=0.00002
>>
pic55.png
  1 Commento
james carsley
james carsley il 25 Lug 2019
The integral produced is the integral of speed,v(mph) so the integral is not in meters like the graph. For the graph I multiplied v by the conversion rate to get m/s, but if in attempt this in the function for the integral it doesnt work. How would I achieve the integral to be in meters?
The integral needed is around 480m.

Accedi per commentare.

Più risposte (1)

james carsley
james carsley il 24 Lug 2019
Thank you very much!

Categorie

Scopri di più su Atomic, Molecular & Optical in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by