function x=bisekcja(a,b)
edop=10^-12;
nmax=100;
e=1;
n=0;
a=1;
b=10;
t = a:0.01:b;
y = F(t);
E = zeros(nmax,1);
while e>edop & n<=nmax
x=(a+b)/2;
if F(a)*F(x)<0
b=x;
else
a=x;
end
e=abs(F(x));
n=n+1;
E(n) = e;
disp([n x e])
end
e
n
subplot(2,1,1);
plot(t,y,'r-','LineWidth',2);
hold on
grid on
plot(x,F(x),'bo');
subplot(2,1,2);
plot(1:n,log10(E(1:n)),'bs-','LineWidth',2);
hold on
grid on
plot([1 n],log10([edop edop]),'k-','LineWidth',2);
function y=F(x)
y=x.^2+3*x-5 + sin(8*x);
-----------------------------------------------------------------------
function x=gauss(A,b)
n=length(A);
m=zeros(n,n);
for k=1:n-1
for i=k+1:n
m(i,k)=(A(i,k))/(A(k,k));
b(i)=(b(i)-m(i,k)*b(k));
for j=1:n
A(i,j)=A(i,j)-(m(i,k)*A(k,j));
end
end
end
for k=n:-1:2
for i=k-1:-1:1
m(i,k)=(A(i,k))/(A(k,k));
b(i)=(b(i)-m(i,k)*b(k));
for j=1:n
A(i,j)=A(i,j)-(m(i,k)*A(k,j));
end
end
end
x=zeros(n,1);
for i=1:n
x(i)=b(i)/A(i,i);
end
-----------------------------------------------
function metodasiecznych
eps = 1e-16;%błąd dopuszczalny
nmax = 11; %maksymalna liczba iteracji+1, minimalnie 2, żeby nie było błędu
n = 1;
xpp = -1; %punkt startowy x0
xp = 5; %punkt startowy x1
e = abs(xp-xpp)/abs(xp);
while (e>eps && n<nmax)
x=xp-F(xp)*(xp-xpp)/(F(xp)-F(xpp));
n = n + 1;
e = abs(x-xp)/abs(x);
xpp=xp;
xp=x;
end
%współrzędne punktu końcowego
wynik=[x F(x)]
%wyznaczanie i rysowanie tej funkcji
t = -10:0.01:10;
y = F(t);
plot(t,y);
hold on
grid on
%rysowanie punktu
plot (x,F(x),'ro');
end
%funkcja której miejsce zerowe wyliczamy
function [z] = F(x)
z = sin(x)+x+1;
end
-------------------------------------------
function aproksymacja
close all
X = [1 2 3 5 8 10];
Y = [1.2 1.7 1.9 5.1 8.2 -10.5];
m=4;
plot(X,Y,'ro');
hold on
grid on
n=size(X,2);
A = zeros(n,m);
b = zeros(n,1);
for i=1:n
% A(i,:)=[X(i)^2 X(i) 1];
for j=1:m
A(i,j)=X(i)^(m-j);
end
b(i)=Y(i);
end
A
b
W=eye(n,n);
W(4,4)=10000;
C = A'*W*A;
d = A'*W*b;
C
d
a=C^-1*d;
x=min(X):0.01:max(X);
% y=a(1)*x.^2+a(2)*x+ a(3);
y=zeros(1,length(x));
for j=1:m
y=y+a(j)*x.^(m-j);
end
plot(x,y,'b-');
--------------------------------------------
function lagrange
X=-5:0.5:4;
Y=sin(X);
n=length(X);
plot(X,Y,'bo','MarkerFaceColor','b')
hold on
x=min(X):0.01:max(X);
m=length(x);
y=zeros(1,m);
for k=1:m
y(k)=p(x(k),n,Y,X);
end
plot(x,y,'r-')
function y=L(x,i,n,X)
a=1;
b=1;
for j=1:n
if j~=i
a=a*(x-X(j));
b=b*(X(i)-X(j));
end
end
y=a/b;
function y=p(x,n,Y,X)
y=0;
for i=1:n
y=y+Y(i)*L(x,i,n,X);
end
--------------------------------------
function Newton
clc
x0=6;
edop=10^-6;
nmax=1000;
n=1;
e=1;
r=1;
X=zeros(nmax,1);
X(1)=x0;
while n<nmax & max([e r])>edop
X(n+1)=X(n) - F(X(n))/Fprim(X(n));
if abs(X(n+1))>edop
e=abs((X(n+1)-X(n))/X(n+1));
else
e=abs(X(n+1)-X(n));
end
r=abs(F(X(n+1))/F(x0));
n=n+1;
disp([n X(n) e r])
end
x=-10:0.01:10;
y=F(x);
plot(x,y,'r-')
hold on
grid on
plot(X(n),F(X(n)),'bo')
function y=F(x)
y=x.*sin(x)-x.^2+5;
function y=Fprim(x)
dx = 0.01;
y = (F(x + dx) - F(x - dx))/(2*dx);
---------------------------------------------
function x = Jacobi(A,b)
n = length(A);
edop = 10^-12;
kmax = 1000;
e = 1;
k = 0;
x0 = ones(n,1);
xp = x0;
xn = zeros(n,1);
while (e>edop & k<=kmax)
for i=1:n
s = 0;
for j=1:n
if i~=j
s = s + A(i,j)*xp(j);
end
end
xn(i) = (b(i) - s)/A(i,i);
end
e = norm(xn - xp)/norm(xn);
k = k + 1;
xp = xn;
k
e
xn
pause
end
x = xn;
e
k
--------------------------------------
function mpotegowa(A)
n=size(A,2);
edop=10^-12;
kmax=1000;
x0=ones(n,1);
xp=x0;
e=1;
k=0;
while e>edop & k<=kmax;
vp=xp/norm(xp);
xn=A*vp;
Lamn=xn'*vp;
if k>0
e=abs((Lamn-Lamp)/Lamn);
end
k=k+1;
Lamp=Lamn;
xp=xn;
end
Lamn
vp
k
e
----------------------------------------
function P = zes1zad2(X,Y)
X=[1 6 9];
Y=[0 -4 7];
plot([X X(1)],[Y Y(1)],'r-','LineWidth',2);
hold on
grid on
a = sqrt( (X(2) - X(1))^2 + (Y(2) - Y(1))^2 );
b = sqrt( (X(3) - X(1))^2 + (Y(3) - Y(1))^2 );
c = sqrt( (X(3) - X(2))^2 + (Y(3) - Y(2))^2 );
p = 0.5*(a+b+c);
P = sqrt(p*(p-a)*(p-b)*(p-c));
------------------------------------------
function katmax = zes3zad2(x0,y0,x1,y1,x2,y2)
plot([x0 x1 x2 x0],[y0 y1 y2 y0],'k-','LineWidth',2);
hold on
grid on
a = sqrt( (x1-x0)^2 + (y1-y0)^2 );
b = sqrt( (x2-x1)^2 + (y2-y1)^2 );
c = sqrt( (x2-x0)^2 + (y2-y0)^2 );
eps = 10^-12;
if abs(a-b)<eps | abs(b-c)<eps | abs(a-c)<eps
if abs(a-b)<eps & abs(b-c)<eps & abs(a-c)<eps
'trójkąt równoboczny'
else
'trójkąt równoramienny'
end
end
if abs(a^2+b^2-c^2)<eps | abs(a^2+c^2-b^2)<eps | abs(b^2+c^2-a^2)<eps
'trójkąt prostokątny'
end
cos1 = (b^2 + c^2 - a^2)/(2*b*c);
cos2 = (a^2 + c^2 - b^2)/(2*a*c);
cos3 = (a^2 + b^2 - c^2)/(2*a*b);
kat1 = acos(cos1)*180/pi;
kat2 = acos(cos2)*180/pi;
kat3 = acos(cos3)*180/pi;
a
b
c
kat1
kat2
kat3
katmax = kat1;
if kat2>katmax
katmax = kat2;
end
if kat3>katmax
katmax = kat3;
end
---------------------------------------
clc
close all
clear all
a=-4;
b=3;
c=0;
x=[];
if a~=0
delta=b^2-4*a*c;
if delta>0
k=2;
x(1)=(-b-sqrt(delta))/(2*a);
x(2)=(-b+sqrt(delta))/(2*a);
elseif delta==0
k=1;
x(1)=-b/(2*a);
else
k=0;
end
else
if b~=0
k=1;
x(1)=-c/b;
else
if c==0
k=0;
disp('nieskonczenie wiele rozwiazan')
else
k=0;
end
end
end
disp('rozwiazania')
x
k
t=-10:0.01:10;
y=a*t.^2+b*t+c;
plot(t,y,'b-','LineWidth',2)
hold on
grid on
for i=1:k
plot(x(i),0,'ro','MarkerFaceColor','r')
end
-------------------------------------------------
opcja = 1;
while opcja~=4
opcja = menu('TYTUL MENU','POLE 1','POLE 2','POLE 3','WYJSCIE');
switch opcja
case 1
'rysunek sinus'
case 2
'rysunek cosinus'
end
end
------------------------------------------------
file = input('Enter name of wave file as a string: ');
[x,fs,bits] = wavread(file);
%
fprintf('Srednia: %.4f \n',mean(x))
fprintf('Odchylenie standardowe: %.4f \n', std(x))
fprintf('Wariancja: %.4f \n', var(x))