clc;
close all;
fun = 'sin(x)';
a = -pi; %poczatek przedzialu
b = pi; %koniec przedzialu
skok = 0.5; %krok
f = inline(fun);
vx = a : skok/110 : b;
vy = f(vx);
plot(vx, vy, 'g');
hold on;
lx = [a];
ly = [0];
calka = 0;
for x = a : skok : b
if(x+skok > b)
skok = b-x;
end
calka = calka + skok*f(x+skok/2);
lx = [lx, x, x+skok, x+skok];
ly = [ly, f(x+skok/2), f(x+skok/2), 0];
end
line ([a b], [0 0]);
calka
line(lx, ly);
axis ([a b min(vy) - 0.01 max(vy)+ 0.01]);
TITLE ('metoda prostakatna');
%paraboli
clc
clear all
close all
a=-pi;
b=pi;
x=a:0.01:b;
f=inline('sin(x)');
y=f(x);
plot(x,y);
hold on
n=100;
h=(b-a)/n
x1=linspace(a,b,n);
y1=f(x1);
I=h/3*(y1(1)+y1(n)+4*sum(y1(1:2:n-1))+2*sum(y1(2:2:n-2)))
for(i=1:n)
line([x1(i),x1(i)],[0,y1(i)]);
end
clc
clear all
close all
a=-pi;
b=pi;
n=100;
f=inline('sin(x)');
x=a:0.01:b;
y=f(x);
plot(x,y);
hold on
d=min(y);
g=max(y);
counto=0;
countb=0;
for i=1:n
xs=rand*(b-a)+a;
ys=rand*(g-d)+d;
if(ys>0)
if(ys<f(xs))
counto=counto+1;
plot(xs,ys,'g *');
else
plot(xs,ys,'r *');
end
elseif(ys<0)
if(ys>f(xs))
countb=countb+1;
plot(xs,ys,'g *');
else
plot(xs,ys,'r *');
end
end
end
I=(counto-countb)/n*(b-a)*(g-d)
%prostokatow
clc
clear all
close all
a=-pi;
b=pi;
x=a:0.01:b;
f=inline('sin(x)');
y=f(x);
plot(x,y);
hold on
n=100;
h=(b-a)/n
x1=linspace(a,b,n);
y1=f(x1);
x1
I=h*(sum(y1)-f(b))
for(i=1:n)
line([x1(i),x1(i)],[0,y1(i)]);
end
%trapezow
clc
clear all
close all
a=-pi;
b=pi;
x=a:0.01:b;
f=inline('sin(x)');
y=f(x);
plot(x,y);
hold on
n=100;
h=(b-a)/n
x1=linspace(a,b,n);
y1=f(x1);
I=h*((y1(1)+y1(n)/2)+sum(y1)-y1(1)-y1(n))
for(i=1:n)
line([x1(i),x1(i)],[0,y1(i)]);
end