Murad Nazari
Murad Nazari

Reputation: 7

Matlab trapezoidal Rule

enter image description here

I created a fas.m script, but I'm getting wrong result.

FUNCTION

 function [fnc2] = fas(x)
if x>=0 && x<1
fnc2 = x^3;
elseif x>=1 && x<2
        fnc2 = 2-((x^2)/5);
elseif x>2
            fnc2 = x^2+x;
 elseif x<0 
     fprintf('x is smaller than 0, function is not defined');

end

TRAPEZOIDAL RULE SUM

clear
clc
h=0.05;
x=0.05;
x0=0;
xn=3;
while x<=2.95
fas(x);
I=0.025*(fas(x0)+2*fas(x)+fas(x0));
x=x+h;
end

Upvotes: 0

Views: 2573

Answers (1)

Rash
Rash

Reputation: 4336

Trapezoidal rule is,

enter image description here

so,

h = 0.05;
x = 0;
I = 0;
while x < 3
   I = I + h * (fas(x) + fas(x + h)) / 2;
   x = x + h;
end
disp(I);

you will get I = 11.3664 while the actual value of I is 10.3667.

Upvotes: 2

Related Questions