ЧМ ЛР5
.docxfunction I = Simpson(x)
I = 0;
h = x(2) - x(1);
for i = 2:1:length(x)
I = I + (f(x(i))+4*f(x(i)-h/2)+f(x(i-1)))*h/6;
end
end
function I = Trapez(x)
I = 0;
h = x(2) - x(1);
for i = 2:1:length(x)
I = I + (f(x(i))+f(x(i-1)))*h/2;
end
end
>> x = 0:0.1:1;
function f = f(x)
f = x^3;
end
>> Trapez(x)
ans = 0.2525
>> e = abs(1/4 - ans)
e = 0.0025
>> Simpson(x)
ans = 0.2500
>> e = abs(1/4 - ans)
e = 0
function f = f(x)
f = x^2;
end
>> Trapez(x)
ans = 0.3350
>> e = abs(1/3 - ans)
e = 0.0017
>> Simpson(x)
ans = 0.3333
>> e = abs(1/3 - ans)
e = 5.5511e-17
function f = f(x)
f = x/2;
end
>> Trapez(x)
ans = 0.2500
>> e = abs(1/4 - ans)
e = 5.5511e-17
>> Simpson(x)
ans = 0.2500
>> e = abs(1/4 - ans)
e = 0
function f = f(x)
f = 1/(1+x^2);
end
>> x = 0:(10^-6):1;
>> Trapez(x)
ans = 0.7854
>> PI = ans*4
PI = 3.1416
>> e = abs(pi-PI)
e = 7.5495e-14
>> Simpson(x)
ans = 0.7854
>> PI = ans*4
PI = 3.1416
>> e = abs(pi-PI)
e = 1.0170e-13
function f = f(x)
f = 1/(1+x^2);
end
>> x = 0:(10^-6):1;
function I = Trapez(x)
I = 0;
h = x(2) - x(1);
for i = 2:1:length(x)
I = I + (f(x(i))+f(x(i-1)))*h/2;
end
end
>> Trapez(x)
>> s = Trapez(x)
s = 0.7854
>> PI = ans*4
PI = 3.1416
>> e = abs(pi-PI)
e = 7.5495e-14
function I = Trapez(x)
I = 0;
h = x(2) - x(1);
for i = 2:1:length(x)
I = I + (f(x(i)-h/2)+f(x(i-1)))*h/4 + (f(x(i))+f(x(i)-h/2))*h/4;
end
end
>> p = Trapez(x)
p = 0.7854
>> e = abs(p-s)
e = 3.4417e-15
>> PI = p*4
PI = 3.1416
>> E = abs(PI-pi)
E = 8.9262e-14