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

%programa para Graficar Funciones: While

This document contains code for plotting functions, finding roots of equations numerically using bisection, false position and Newton-Raphson methods, and approximating functions using polynomial interpolation. Specifically: 1) It plots quadratic and nonlinear functions and finds roots of equations using bisection. 2) It finds roots of nonlinear equations using false position and Newton-Raphson. 3) It approximates functions using polynomial interpolation methods like Lagrange and Newton polynomials.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views5 pages

%programa para Graficar Funciones: While

This document contains code for plotting functions, finding roots of equations numerically using bisection, false position and Newton-Raphson methods, and approximating functions using polynomial interpolation. Specifically: 1) It plots quadratic and nonlinear functions and finds roots of equations using bisection. 2) It finds roots of nonlinear equations using false position and Newton-Raphson. 3) It approximates functions using polynomial interpolation methods like Lagrange and Newton polynomials.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

%Programa para graficar funciones

subplot(2,2,1)
clc,clear
x=-3:0.1:6;
y=x.^2-5*x+2;
plot(x,y)
grid on
%Otra forma de graficar
subplot(2,2,2)
fplot('x^2-5*x+2',[-3,6])
hold on
fplot('0',[-3,6])
grid on
title('Funcion cuadratica')
xlabel('x')
ylabel('y')
hold off

%Metoo de Biseccion
clc,clear
a=0;b=1;tol=0.1;MEP=50;
f=inline('x^2-5*x+2')
disp(' raiz1 MEP')
disp('========================')
while(MEP>tol)
c=(a+b)/2;
MEP=(b-a)/2;
fprintf('%12.7f %12.7f\n',c,MEP)
if f(a)*f(c)<0
b=c;
else
a=c;
end
end
a=4;b=5;tol=0.1;MEP=50;
disp(' raiz MEP')
disp('========================')
while(MEP>tol)
c=(a+b)/2;
MEP=(b-a)/2;
fprintf('%12.7f %12.7f\n',c,MEP)
if f(a)*f(c)<0
b=c;
else
a=c;
end
end

%Metoo de Biseccion
clc,clear
a=2;b=3;tol=0.1;MEP=50;
f=inline('3*sin(x)-2*x+2')
disp(' raiz1 MEP')
disp('========================')
while(MEP>tol)
c=(a+b)/2;
MEP=(b-a)/2;
fprintf('%12.7f %12.7f\n',c,MEP)
if f(a)*f(c)<0
b=c;
else
a=c;
end
end

%Metodo de Biseccion
clc,clear
a=2;b=3;tol=0.5*10^(-3);ER=50;
f=inline('3*sin(x)-2*x+2')
disp(' raiz1 MEP')
disp('========================')
while(ER>tol)
c=(a+b)/2;
MEP=(b-a)/2;
ER=MEP/c;
fprintf('%12.3f %12.7f\n',c,ER)
if f(a)*f(c)<0
b=c;
else
a=c;
end
end

PROGRAMA1
clc,clear
%Grafica de la funcion
%fplot('3*sin(x)-2*x+1') Mensaje error
fplot(@(x) 3*sin(x)-2*x+1,[-3 3])
hold on
fplot(@(x) 0*x,[-3 3])
grid on
title('Funcion no lineal')
xlabel('X')
ylabel('Y')
hold off
PROGRAMA 2
%METODO DE FALSA POSICION
clc,clear
f=inline('3*sin(x)-2*x+1')
a=1;b=2;tol=0.01;error=10;
%la iteracion
c=(a*f(b)-b*f(a))/(f(b)-f(a));
if f(a)*f(c)<0
b=c;
else
a=c;
end
disp('Raiz aprox. Error Absoluto')
while(error>tol)
c1=(a*f(b)-b*f(a))/(f(b)-f(a));
error=abs(c1-c);
c=c1;
disp([c,error])
if f(a)*f(c)<0
b=c;
else
a=c;
end
end

PROGRAMA 3
%Grafica sistema de ecuaciones
subplot(2,2,2)
fplot(@(x) 2*x+1,[-3,3])
hold on
fplot(@(x) 3*sin(x)+2,[-3,3])
grid on
title ('Sistema no lineal')
xlabel('X')
ylabel('Y')
hold off

PROGRAMA 4
%METODO DEL PUNTO FIJO G(x)
clc,clear
x0=2;tol=0.01;error=10
G=inline('(3*sin(x)+1)/2')
dG=inline('(3*cos(x))/2')
if abs(dG(x0))<1
disp('Si converge')
disp('Raiz Aprox Error Absoluto')
while(error>tol)
x1=G(x0);
error=abs(x1-x0);
disp([x1,error])
x0=x1;
end
else
disp('diverge')
end
%METODO DEL NEWTON RAPHSON
clc,clear
x0=2;tol=0.01;error1=10;error2=10
f=inline('3*sin(x)-2*x+1')
df=inline('3*cos(x)-2')
disp('Raiz Aprox. Error Relativo')
while(error1>tol | error2>tol)
x1=x0-f(x0)/df(x0);
error1=abs((x1-x0)/x1);
error2=abs(f(x1));
fprintf('%10.6f %10.7f %15.12f\n',x1,error1,error2)
x0=x1;
end

%METODO DE LA SECANTE
clc,clear
x0=2;x1=1.5;tol=0.01;error1=10;error2=10
f=inline('3*sin(x)-2*x+1')
disp('Raiz Aprox. Error Relativo')
while(error1>tol | error2>tol)
x2=x1-f(x1)*(x1-x0)/(f(x1)-f(x0));
error1=abs((x2-x1)/x2);
error2=abs(f(x2));
fprintf('%10.6f %10.7f %15.12f\n',x2,error1,error2)
x0=x1;x1=x2;
end

%METODO DE APROXIMACION SIMPLE


%
clc,clear
x=[140;180;220;240]
y=[12800;7500;5000;3800]
xinterp=200
A=[ones(4,1) x x.^2 x.^3];
%A.ai=y
a=inv(A)*y
fprintf('P(x)=%.3f+%.3f x+%.3f x^2+%3f x^3\n',a)
Px=a(1)+a(2)*xinterp+a(3)*xinterp^2+a(4)*xinterp^3;
fprintf('P(%d)=%.3f\n',xinterp,Px)
plot(x,y,'*')
xx=140:0.01:240;
yy=a(1)+a(2)*xx+a(3)*xx.^2+a(4)*xx.^3;
hold on
plot(xx,yy)
plot(xinterp,Px,'o')
%Polinomio de Lagrange
%METODO DE APROXIMACION SIMPL
clc,clear
x=[10,20,30]
y=[12,28,42]
xinterp=16
n=length(x)-1 %grado del polinomio
for i=1:n+1
L(i)=1;
for j=1:n+1
if i~=j
L(i)=L(i)*(xinterp-x(j))/(x(i)-x(j));
end
end
end
L
Px=sum(L.*y);
%Polinomio de Newton
%primer Grado
clc,clear
x=[10 20]
y=[12 28]
xinterp=16
a0=y(1)
a1=(y(2)-y(1))/(x(2)-x(1))
fprintf('P(x)=%f+%f(x-%d)\n',a0,a1,x(1))
Px=a0+a1*(xinterp-x(1))
fprintf('P(%d)=%.3f\n',xinterp,Px)

You might also like