function[y]=f(x)
y=1/(1+(x*x));
Save the above function as f.m.
function[sum]=trap(a,b,n)
h=(b-a)/n;
for i=1:n+1
x(i)=a;
a=a+h;
end
sum=0;
sum=sum+f(x(1))+f(x(n+1));
for i=2:n
sum=sum+2*f(x(i));
end
sum=sum*(h/2);
Arguments sent to the function are:- trap(lower bound,upper bound,number of sub intervals)
0 comments:
Post a Comment