Unrecognized function or variable 'trapizoidal'.

15 Ansichten (letzte 30 Tage)
aqil hilman
aqil hilman am 15 Dez. 2020
Beantwortet: Alan Stevens am 15 Dez. 2020
%Matlab code for finding integration using different method
clear all
close all
% function for integration
f1=@(v) (5000.*v)./(8.276.*v.^2+2000);
%upper and lower limit
a=0; b=10;
%exact integral
ext_int=integral(f1,a,b);
%Function for which integration have to do
fprintf('\nFunction for which integration have to do f(v)=\n')
disp(f1)
fprintf('Upper and lower limit of integration [%2.2f %2.2f]\n\n',a,b)
fprintf('Exact integral for given function is %f\n',ext_int)
%Integration using Trapizoidal, Simpson 1/3 and Simpson 3/8 method
n=1;
val_trap=trapizoidal(f1,a,b,n);
err=(abs((val_trap-ext_int)/ext_int))*100;
fprintf('Relative percent Error in trapizoidal rule for n=%d is %f\n',n,err)
val_simp13=Simp13_int(f1,a,b,n);
err=(abs((val_simp13-ext_int)/ext_int))*100;
fprintf('Relative percent Error in Simpson 1/3 rule for n=%d is %f\n',n,err)
val_simp38=Simp38_int(f1,a,b,n);
err=(abs((val_simp38-ext_int)/ext_int))*100;
fprintf('Relative percent Error in Simpson 3/8 rule for n=%d is %f\n',n,err)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Integration using Trapizoidal, Simpson 1/3 and Simpson 3/8 method
n=20;
val_trap=trapizoidal(f1,a,b,n);
err=(abs((val_trap-ext_int)/ext_int))*100;
fprintf('Relative percent Error in trapizoidal rule for n=%d is %f\n',n,err)
val_simp13=Simp13_int(f1,a,b,n);
err=(abs((val_simp13-ext_int)/ext_int))*100;
fprintf('Relative percent Error in Simpson 1/3 rule for n=%d is %f\n',n,err)
val_simp38=Simp38_int(f1,a,b,n+1);
err=(abs((val_simp38-ext_int)/ext_int))*100;
fprintf('Relative percent Error in Simpson 3/8 rule for n=%d is %f\n',n+1,err)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Matlab function for Trapizoidal integration
function val=trapizoidal(func,a,b,N)
% func is the function for integration
% a is the lower limit of integration
% b is the upper limit of integration
% N number of rectangles to be used
val=0;
%splits interval a to b into N+1 subintervals
xx=linspace(a,b,N+1);
dx=xx(2)-xx(1); %x interval
%loop for Trapizoidal integration
for i=2:length(xx)-1
xx1=xx(i);
val=val+dx*double(func(xx1));
end
val=val+dx*(0.5*double(func(xx(1)))+0.5*double(func(xx(end))));
fprintf('\t Integral using Trapizoidal method\n')
fprintf('The value of integral from a=%f to b=%f\n',a,b)
fprintf('using %d equally spaced divisions is : %2.15f\n',N,val)
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Matlab function for Simpson 1/3 Method
function val=Simp13_int(f,a,b,n)
%f=function for which integration have to do
%a=upper limit of integration
%b=lower limit of integration
%n=number of subintervals
zs=f(a)+f(b); %simpson integration
%all x values for given subinterval
xx=linspace(a,b,n+1);
dx=(xx(2)-xx(1)); %x interval
%Simpson Algorithm for n equally spaced interval
for i=2:n
if mod(i,2)==0
zs=zs+4*f(xx(i));
else
zs=zs+2*f(xx(i));
end
end
%result using Simpson rule
val=double((dx/3)*zs);
fprintf('\t Integral using Simpson 1/3 method\n')
fprintf('The value of integral from a=%f to b=%f\n',a,b)
fprintf('using %d equally spaced divisions is : %2.15f\n',n,val)
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Matlab function forSimpson 3/8 Method
function val=Simp38_int(f,a,b,n)
% f is the function for integration
% a is the lower limit of integration
% b is the upper limit of integration
% n is the number of trapizoidal interval in [a,b]
%splits interval a to b into n+1 subintervals
xx=linspace(a,b,n+1);
dx=(xx(2)-xx(1)); %x interval
val=f(a)+f(b);
%loop for trapizoidal integration
for i=2:n
if mod(i-1,3)==0
val=val+2*double(f(xx(i)));
else
val=val+3*double(f(xx(i)));
end
end
%result using midpoint integration method
val=(3/8)*dx*val;
fprintf('\n\t Integral using Simpson 3/8 method\n')
fprintf('The value of integral from a=%f to b=%f\n',a,b)
fprintf('using %d equally spaced divisions is : %2.15f\n',n,val)
end
%%%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%%%
  2 Kommentare
RenatoL
RenatoL am 15 Dez. 2020
I think it would be better if you save the functions trapizoidal, Simp13_int, and Simp38_int on separate .m files

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Alan Stevens
Alan Stevens am 15 Dez. 2020
This works for me exactly as you have listed it! This is the output:
Function for which integration have to do f(v)=
@(v)(5000.*v)./(8.276.*v.^2+2000)
Upper and lower limit of integration [0.00 10.00]
Exact integral for given function is 104.604010
Integral using Trapizoidal method
The value of integral from a=0.000000 to b=10.000000
using 1 equally spaced divisions is : 88.414202857547053
Relative percent Error in trapizoidal rule for n=1 is 15.477234
Integral using Simpson 1/3 method
The value of integral from a=0.000000 to b=10.000000
using 1 equally spaced divisions is : 58.942801905031367
Relative percent Error in Simpson 1/3 rule for n=1 is 43.651489
Integral using Simpson 3/8 method
The value of integral from a=0.000000 to b=10.000000
using 1 equally spaced divisions is : 66.310652143160283
Relative percent Error in Simpson 3/8 rule for n=1 is 36.607925
Integral using Trapizoidal method
The value of integral from a=0.000000 to b=10.000000
using 20 equally spaced divisions is : 104.567194078904976
Relative percent Error in trapizoidal rule for n=20 is 0.035196
Integral using Simpson 1/3 method
The value of integral from a=0.000000 to b=10.000000
using 20 equally spaced divisions is : 104.604038713787162
Relative percent Error in Simpson 1/3 rule for n=20 is 0.000027
Integral using Simpson 3/8 method
The value of integral from a=0.000000 to b=10.000000
using 21 equally spaced divisions is : 104.604063246835111
Relative percent Error in Simpson 3/8 rule for n=21 is 0.000051

Weitere Antworten (0)

Kategorien

Mehr zu Numerical Integration and Differential Equations finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by