How to define the function to use for the composite trapezoidal rule?
4 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
KALYAN ACHARJYA
2019-7-24
编辑:KALYAN ACHARJYA
2019-7-24
"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
>>
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Atomic, Molecular & Optical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!