jueves, 7 de febrero de 2013

spline

x0=input('introdzca el valor x0: ');
f0=input('introdzca el valor f0: ');
x1=input('introdzca el valor x1: ');
f1=input('introdzca el valor f1: ');
x2=input('introdzca el valor x2: ');
f2=input('introdzca el valor f2: ');
x3=input('introdzca el valor x3: ');
f3=input('introdzca el valor f3: ');
%primera ecuacion

a=(x1-x0);
b=2*(x2-x0);
c=(x2-x1);
r=(6/(x2-x1))*(f2-f1);
r1= r+(6/(x1-x0))*(f0-f1);

%segunda ecuacion
a1=(x2-x1);
b1=2*(x3-x1);
c1=(x3-x2);
d=(6/(x3-x2))*(f3-f2);
r2= d+(6/(x2-x1))*(f1-f2);
%igualacion

h2=(c*r2-b1*r1)/(c*a1-b1*b);
h3=(r1-b*h2)/c;
%extremos
h=0;
h4=0;
%fin extremo

%primera funcion
t1=h/(6*(x1-x0));
t2=(h2/(6*(x1-x0)));
t3=((f0/(x1-x0))-((h*(x1-x0)/6)));
t4=((f1/(x1-x0))-(h2*(x1-x0))/6);
if t1==0
fprintf('%f(x-%.1f)^3+%f(%.1f-x)+%f(x-%.1f)',t2,x0,t3,x1,t4,x0);
elseif t2==0
fprintf('%f(%.1f-x)^3+%f(%.1f-x)+%f(x-%.1f)',t1,x1,t3,x1,t4,x0);
elseif t3==0
    fprintf('%f(%.1f-x)^3+ %f(x-%.1f)^3+%f(x-%.1f)',t1,x1,t2,x0,t4,x0);
elseif t4==0
    fprintf('%f(%.1f-x)^3+ %f(x-%.1f)^3+%f(%.1f-x)',t1,x1,t2,x0,t3,x1);
else
    fprintf('%f(%.1f-x)^3+ %f(x-%.1f)^3+%f(%.1f-x)+%f(x-%.1f)',t1,x1,t2,x0,t3,x1,t4,x0);
end
%para grafica
%ojo esta es
    v = (x0:0.3:x1)';
  s=[v];
 p=[t1*(4.5-v).^3+ t2*(v-3).^3+t3*(4.5-v)+t4*(v-3)];
plot(s,p)




   
   

x=[x0,x1,x2,x3];
y=[f0,f1,f2,f3];

%segunda funcion
fprintf('\n')
p1=h2/(6*(x2-x1));
p2=(h3/(6*(x2-x1)));
p3=((f0/(x2-x1))-((h2*(x2-x1)/6)));
p4=((f1/(x2-x1))-(h3*(x2-x1))/6);

if p1==0
fprintf('%f(x-%.1f)^3+%f(%.1f-x)+%f(x-%.1f)',p2,x1,p3,x2,p4,x1);
elseif p2==0
fprintf('%f(%.1f-x)^3+%f(%.1f-x)+%f(x-%.1f)',p1,x2,p3,x2,p4,x1);
elseif p3==0
    fprintf('%f(%.1f-x)^3+ %f(x-%.1f)^3+%f(x-%.1f)',p1,x2,p2,x1,p4,x1);
elseif p4==0
    fprintf('%f(%.1f-x)^3+ %f(x-%.1f)^3+%f(%.1f-x)',p1,x2,p2,x1,p3,x2);
else
    fprintf('%f(%.1f-x)^3+ %f(x-%.1f)^3+%f(%.1f-x)+%f(x-%.1f)',p1,x2,p2,x1,p3,x2,p4,x1);
end
fprintf('\n')

%ojo esta es
    l = (x1:0.3:x2)';
  k=[l];
 g=[p1*(x2-l).^3+ p2*(l-x1).^3+p3*(x2-l)+p4*(l-x1)];
plot(k,g)



fprintf('\n')
a1=h3/(6*(x3-x2));
a2=(h4/(6*(x3-x2)));
a3=((f0/(x3-x2))-((h3*(x3-x2)/6)));
a4=((f1/(x3-x2))-(h4*(x3-x2))/6);

%tercera funcion

if a1==0
fprintf('(%f(x-%.1f)^3)+(%f(%.1f-x))+(%f(x-%.1f))',a2,x2,a3,x3,a4,x2);
elseif a2==0
fprintf('(%f(%.1f-x)^3)+(%f(%.1f-x)+%f(x-%.1f))',a1,x3,a3,x3,a3,x2);
elseif a3==0
    fprintf('(%f(%.1f-x)^3)+ (%f(x-%.1f)^3)+(%f(x-%.1f))',a1,x3,a2,x2,a4,x2);
elseif a4==0
    fprintf('(%f(%.1f-x)^3)+ (%f(x-%.1f)^3)+(%f(%.1f-x))',a1,x3,a2,x2,a3,x3);
else
    fprintf('(%f(%.1f-x)^3)+ (%f(x-%.1f)^3)+(%f(%.1f-x))+(%f(x-%.1f))',a1,x3,a2,x2,a3,x3,a4,x2);
end
fprintf('\n')


%plot (x,y,'*','MarkerEdgeColor','r','LineWidth',1);



2 comentarios: