You are on page 1of 10

Ejercicio 2

clear all

clc

format short

a0=-1;

xn=[1987 1988 1989];

yn=[53 71 91];

n=length(xn);

x=sym('x');

p2=0;

for i=1:n

L=1;

for j=1:n

if j~=i

L=L*(x-xn(j))/(xn(i)-xn(j));

end

end

p2=p2+L*yn(i);

end

p2=simplify(p2);

pretty(p2)

xr=1985;

yr=subs(p2,xr);

xn(n+1)=xr;

yn(n+1)=yr;

subplot (2,1,1);

plot(xn,yn,'*')

hold on

grid on

subplot(2,1,2);

hold on
grid on

ezplot(p2,[xr-1,xn(n)+1]);

plot(xr,yr,'*');

disp('la poblacion en 1985 es');

disp(yr);

Ejercicio 4

clear all

clc

format short

xn=[0 3 2];

yn=[9 10 9];

n=length(xn);

x=sym('x');

p2=0;

for i=1:n

L=1;

for j=1:n

if j~=i

L=L*(x-xn(j))/(xn(i)-xn(j));

end

end

p2=p2+L*yn(i);

end

p2=simplify(p2);

pretty(p2)

subplot (2,1,1);

plot(xn,yn,'*')

hold on

grid on

subplot(2,1,2);
hold on

grid on

ezplot(p2,[xn(1)-1,xn(n)+1]);

Ejercicio 6

clear all

clc

format short

a0=-1;

xn=[1 3 5];

yn=[-3 5 21];

n=length(xn);

x=sym('x');

p2=0;

for i=1:n

L=1;

for j=1:n

if j~=i

L=L*(x-xn(j))/(xn(i)-xn(j));

end

end

p2=p2+L*yn(i);

end

p2=simplify(p2);

pretty(p2)

xa=4;

ya=subs(p2,xa);

xb=2;

yb=subs(p2,xb);

eb=abs(-1-yb);
subplot (2,1,1);

plot(xn,yn,'*')

hold on

grid on

plot(xb,-1,'*');

subplot(2,1,2);

hold on

grid on

ezplot(p2,[xn(1),xn(n)]);

plot(xb,-1,'*');

disp('a.) f(4)es: ');

disp(ya);

disp('b.) el error de f(2)es: ');

disp(eb);

Ejercicio 8

clear all

clc

format short

a0=-1;

xn=[0 1 4];

yn=[1 -3 5];

n=length(xn);

x=sym('x');

p2=0;

for i=1:n

L=1;

for j=1:n

if j~=i

L=L*(x-xn(j))/(xn(i)-xn(j));

end
end

p2=p2+L*yn(i);

end

p2=simplify(p2);

pretty(p2)

xa=0.5;

ya=subs(p2,xa);

xb=2;

yb=subs(p2,xb);

m=(5-1)/(4-0);

y=m*x+1;

yal=subs(y,0.5);

ybl=subs(y,2);

subplot (3,1,1);

plot(xn,yn,'*')

hold on

grid on

subplot(3,1,3);

hold on

grid on

ezplot(p2,[xn(1),xn(n)]);

subplot(3,1,2);

hold on

grid on

ezplot(y,[xn(1),xn(n)]);

disp('a.) f(4)es: ');

disp(yal);

disp('a.) f(2)es: ');

disp(ybl);

disp('b.) f(4)es: ');

disp(ya);
disp('b.) f(2)es: ');

disp(yb);

Ejercicio 10

clear all

clc

format short

a0=-1;

xn=[1965 1975 1985];

yn=[3340 4080 4850];

n=length(xn);

x=sym('x');

p2=0;

for i=1:n

L=1;

for j=1:n

if j~=i

L=L*(x-xn(j))/(xn(i)-xn(j));

end

end

p2=p2+L*yn(i);

end

p2=simplify(p2);

pretty(p2)

xa=1990;

ya=vpa(subs(p2,xa));

c=0;

syms y

xr=solve(y==p2,x);

r=subs(xr,6500);

for i=1:2

if r(i)>1990
re=round(vpa(r(i)));

end

end

subplot (2,1,1);

plot(xn,yn,'*')

hold on

grid on

subplot(2,1,2);

hold on

grid on

ezplot(p2,[xn(1),2007]);

disp('a.) f(4)es: ');

plot(2005,6500,'*')

disp(ya);

disp('b.) la poblacion es 6500 millones en: ');

disp(re);

Ejercicio 12

clear all

clc

format short

a0=-1;

x=sym('x');

xn=[1970 1980 1987];

yn=[2.87 3.32 3.49];

m=(3.49-3.32)/(1987-1980);

y=m*x;

c=2.76-1962*m;

y=y+c;

yal=vpa(subs(y,1990));

n=length(xn);
p2=0;

for i=1:n

L=1;

for j=1:n

if j~=i

L=L*(x-xn(j))/(xn(i)-xn(j));

end

end

p2=p2+L*yn(i);

end

p2=simplify(p2);

pretty(p2)

xa=1990;

ya=vpa(subs(p2,xa));

subplot (2,1,1);

plot(xn,yn,'*')

hold on

grid on

subplot(2,1,2);

hold on

grid on

ezplot(p2,[xn(1),xn(n)]);

disp('a.) las calorias en 1990 segun la funcion lineal es: ');

disp(yal);

disp('b.)las calorias en 1990 segun la funcion cuadratica es: ');

disp(ya);

Ejercicio 14
xbi=input('Ingrese el intervalo superior: ');

tol=input('Ingrese el porcentaje de error: ');

syms x;

f=input('Ingrese la funciòn: ');

i=1;

f1=subs(f,x,xai);

f2=subs(f,x,xbi);

ea(i)=100;

if f1*f2 < 0

xa(i)=xai; f1=subs(f,x,xa(i));

xb(i)=xbi; f2=subs(f,x,xb(i));

xr(i)=(xa(i)+xb(i))/2; f3=subs(f,x,xr(i));

fprintf('It. Xa Xr Xb Error aprox \n');

fprintf('%2d \t %11.7f \t %11.7f \t %11.7f \n',i,xa(i),xr(i),xb(i));

while abs(ea(i)) >= tol,

if f1*f3<0

xa(i+1)=xa(i);f1=subs(f,x,xa(i+1));

xb(i+1)=xr(i);f2=subs(f,x,xb(i+1));

end

if f1*f3> 0

xa(i+1)=xr(i);f1=subs(f,x,xa(i+1));

xb(i+1)=xb(i);f2=subs(f,x,xb(i+1));

end

xr(i+1)=(xa(i+1)+xb(i+1))/2;

f3=subs(f,x,xr(i+1));

ea(i+1)=abs((xr(i+1)-xr(i))/(xr(i+1))*100);

fprintf('%2d \t %11.7f \t %11.7f \t %11.7f \t %7.3f \n',...

i+1,xa(i+1),xr(i+1),xb(i+1),ea(i+1));

i=i+1;

end

else
fprintf('No existe una raíz en ese intervalo');

end

You might also like