Buscar

Exemplo de códigos (todos os métodos)

Prévia do material em texto

function [y]=f1(x)
 //Função
 y=cos(x)-x
endfunction
function [y]=f2(x)
 //Derivada da função
 y=-sin(x)-1
endfunction
function [p]=bissecao(f,a,b,N,e1,e2,e3)
 //Método da bisseção
 for i=1:N
 p=(a+b)/2
 
 disp([i a b p])
 
 if abs(f(p))<=e1 then break end
 
 if i>1
 if abs((p-p0))<e2 then break end
 if abs((p-p0)/p)<e3 then break end
 end
 
 if f(a)*f(p)<0 then
 b=p
 else
 a=p
 end
 p0=p
 end
endfunction
function [p]=pfalsa(f,a,b,N,e1,e2,e3)
 //Método da posição falsa
 
 for i=1:N
 p=(a*f(b) - b*f(a))/(f(b)-f(a))
 
 disp([i a b p])
 
 if abs(f(p))<=e1 then break end
 
 if i>1
 if abs((p-p0))<e2 then break end
 if abs((p-p0)/p)<e3 then break end
 end
 
 if f(a)*f(p)<0 then
 b=p
 else
 a=p
 end
 p0=p
 end
endfunction
function [xn1]=secante(f,xnm1,xn,N,e1,e2,e3)
 //Método das secantes
 
 for i=1:N
 m=(f(xn) - f(xnm1))/(xn-xnm1)
 xn1=xn-f(xn)/m
 
 disp([i xnm1 xn xn1])
 
 if abs(f(xn1))<=e1 then break end
 if abs((xn1-xn))<e2 then break end
 if abs((xn1-xn)/xn1)<e3 then break end
 
 xnm1=xn
 xn=xn1
 end
endfunction
function [xn1]=newton(f,df,xn,N,e1,e2,e3)
 //Método de Newton
 
 for i=1:N
 m=df(xn) 
 xn1=xn-f(xn)/m
 
 disp([i xn m xn1])
 
 if abs(f(xn1))<=e1 then break end
 if abs((xn1-xn))<e2 then break end
 if abs((xn1-xn)/xn1)<e3 then break end
 
 xn=xn1
 end
endfunction

Outros materiais

Materiais relacionados

Perguntas relacionadas

Perguntas Recentes