0% found this document useful (0 votes)
58 views5 pages

Function: % Ako Su Razlicite Duzine Dopuniti Nulama

The document contains Matlab code for various numerical methods including: 1) Polynomial operations like subtraction and multiplication of polynomials. 2) Vector norms and matrix-vector multiplication. 3) Gaussian elimination method for solving systems of linear equations. 4) Jacobi and Gauss-Seidel iterative methods for solving linear systems. 5) Root finding methods like bisection, secant, and tangent methods. 6) Polynomial interpolation using Lagrange polynomials. 7) Linear regression using the method of least squares.

Uploaded by

OnlyForYouMyDear
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views5 pages

Function: % Ako Su Razlicite Duzine Dopuniti Nulama

The document contains Matlab code for various numerical methods including: 1) Polynomial operations like subtraction and multiplication of polynomials. 2) Vector norms and matrix-vector multiplication. 3) Gaussian elimination method for solving systems of linear equations. 4) Jacobi and Gauss-Seidel iterative methods for solving linear systems. 5) Root finding methods like bisection, secant, and tangent methods. 6) Polynomial interpolation using Lagrange polynomials. 7) Linear regression using the method of least squares.

Uploaded by

OnlyForYouMyDear
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

vezba1 - matlab osnove

function g = razlikapolinoma(g1,g2) % ako su razlicite duzine dopuniti nulama


n=length(g1);
g=zeros(1,n);
for i=1:n
g(i)=g1(i)-g2(i);
end
function p=mnozenjepolinoma(a,b)
n=length(a);
m=length(b);
p=zeros(1,m+n-1);
for i=1:n
for j=0:m-1
p(i+j)=p(i+j)+a(i)*b(j+1);
end
end
function v = vnorma(x,norma)
% x - vektor
% norma = {1, 2, inf}
n=length(x);
v=0;
if(norma==1)
for i=1:n
v=v+abs(x(i));
end
elseif(norma==2)
for i=1:n
v=v+(x(i)*x(i));
end
v=sqrt(v);
elseif(norma==inf)
v=max(abs(x));
else
disp('GRESKA: Nepostojeca norma.');
end
function c = mmv(A,b) % mnozenje matrice i vektorora
n=length(A);
c=zeros(1,n);
for i=1:n
for j=1:n
c(i)= c(i) + A(i,j)*b(j);
end
end
c=c';

vezba2 - gauss
function x = gauss(A,b)
n=length(A);
for k=1:n
[A,b]=swrow(A,b,k);
for i=k+1:n
m = A(i,k)/A(k,k);
for j=k:n
A(i,j)=A(i,j)-m*A(k,j);
end
b(i)=b(i)-m*b(k);
end
end

x=zeros(1,n);
for i=n:-1:1
s=0;
for k=i+1:n
s=s+A(i,k)*x(k);
end
x(i)=(b(i)-s)/A(i,i);
end
function [A,b] = swrow(A,b,k) % red sa max elementom postavi na prvi u ostatku matrices
l=maxelem(A,k);
n=length(A);
for j=k:n
temp=A(k,j);
A(k,j)=A(l,j);
A(l,j)=temp;
end
temp=b(k);
b(k)=b(l);
b(l)=temp;
function l = maxelem(A,k) % odredjuje max element u koloni k
n=length(X);
l=k;
for i=k:n
if(abs(A(i,k))>A(l,k))
l=i;
end
end

vezba3 - gz, jakobi


function [xkplus1] = gz(A,b,itMax,err)
n=length(A);
xk=zeros(1,n);
xkplus1=zeros(1,n);
i
for it=1:itMax
novo
for i=1:n
s1=0;
i
s2=0;
for j=1:i-1
s1=s1+A(i,j)*xkplus1(j);
end
for j=i+1:n
s2=s2+A(i,j)*xk(j);
end
xkplus1(i)=(b(i)-s1-s2)/A(i,i);
end
e=0;
for i=1:n
e=e+(xkplus1(i)-xk(i))^2;
end
if(e<err)
return;
end
xk=xkplus1;
end
function x = jakobi(A,b,x0,itMax,err)
if sdd(A)==0
disp('matrica nije strogo dijagonalno dominantna')
else
n=length(A);
x = zeros(1,n);

i 1

b ( aij x
j 1

novo
j

aii

aij x staro
)
j
j i 1

for it=1:itMax
for i=1:n
s=0;
for j=1:i-1
s=s+A(i,j)*x0(j);
end
for j=i+1:n
s=s+A(i,j)*x0(j);
end
x(i)=(b(i)-s)/A(i,i);
end
e=0;
for i=1:n
e=e+(x(i)-x0(i))^2;
end
if(e<err)
return;
end
x0=x;
end

i 1

xinovo

bi ( aij x
j 1

staro
j

aij x staro
)
j
j i 1

aii

end
function[flag] = sdd(A)
%flag = 1 sve je u redu
%flag = 0 doslo je do greske
[n,m]=size(A);
flag=1;
s=zeros(1,m);
for i=1:n
for j=1:m
if j~=i
s(i)=s(i)+A(i,j);
end
if A(i,i)<s(i)
flag=0;
return;
end
end
end
vezba4 polovljenje, secica, tangenta
function x = polovljenje(a,b,err,itMax,func)
n=length(a);
x=zeros(1,n);
for i=1:n
for it=1:itMax
ya=feval(func,a(i));
yb=feval(func,b(i));
xm=(a(i)+b(i))/2;
ym=feval(func,xm);
if(abs(ym)<err||(abs(b(i)-a(i))<err))
x(i)=xm;
break;
end
if(ya*ym<0)
b(i)=xm;
else
a(i)=xm;
end
end
end

aii aij
j 1
j i

function x = secica(a,b,err,itMax,func)
n=length(a);
x=zeros(1,n);
for i=1:n
for it=1:itMax
ya=feval(func,a(i));
yb=feval(func,b(i));
xa=(yb-ya)/(b(i)-a(i));
xb=ya-xa*a(i);
xm=-xb/xa;
ym=feval(func, xm);
if(abs(ym)<err||(abs(a(i)-b(i))<err))
x(i)=xm;
break;
end
if(ya*ym<0)
b(i)=xm;
else
a(i)=xm;
end
end
end
function x=tangenta(a,b,err,itMax,func,funcprime)
n=length(a);
x=zeros(1,n);
for i=1:n
for it=1:itMax
ya=feval(func,a(i));
yb=feval(func,b(i));
ya_izvod=feval(funcprime,a(i));
xm=a(i)-(ya/ya_izvod);
ym=feval(func,xm);
if((abs(ym)<err || abs(a(i)-b(i))<err))
x(i)=xm;
break;
end
if(ya*ym<0)
b(i)=xm;
else
a(i)=xm;
end
end
end
function gp=izvod(g) % g,gp - koeficijenti polinoma
n=length(g);
gp=zeros(1,n-1);
if n>1
for i=1:n-1
gp(i)=g(i)*(n-i);
end;
end;
% funcprime = polyval(gp,x)

vezba5 - lagranz interpolacija


function g = LI(x,y)
n=length(x);
g=zeros(1,n);
for i=1:n
p=1;
q=1;
for j=1:n

P( x) ( yi
i 1

j 1, j i

(x x j )
( xi x j )

if(j~=i)
p=p*(x(i)-x(j));
q=conv(q,[1 -x(j)]);
end
end
g=g+y(i)*q/p;
end
function y = LIy(xp,yp,x)
n=length(x);
y=zeros(1,n);
for i=1:n
p=yp(i);
for j=1:n
if i~=j
p=p.*(x-xp(j))/(xp(i)-xp(j));
end
end
y=y+p;
end

vezba6 - najmanji kvadrati


function f = NK(x,y,n)
f=zeros(1,n);
m=length(x);
X=zeros(m,n+1);
for j=1:n+1
for i=1:m
X(i,j)=x(i)^(j-1);
end
end
% 1.nacin
koef=(X'*X)\(X'*y');
% 2.nacin
% koef = gauss(X'*X,X'*y')
for i=1:n+1
f(i)=koef(n+2-i);
end

1
x

x
1
2
...
...
X
...
...

1 x
m1

1
xm

x2
1
x2
2
...

...
...
...

...
...
x2
...
m1
2
xm
...

xn
m1
n
xm
( mn1 )

xn
1
xn
2

X T X a X T Y

You might also like