Double interpolation in Matlab
4 views (last 30 days)
Show older comments
Hi,
I want to ask you somehting about interpolation in Matlab. I want to know if it's possible to do two different interpolations in the same line. I mean, for example, at the beginning do a linear interpolation and at the middle, approximately, do another kind of interpolation, like spline.
The main problem is that I've done a linear interpolation and it's perfect at the beginning but after some point, I think it would be better another type. If this is possible, how can I code where I want to change it?
Thanks a lot in advance and greetings,
Emma
0 Comments
Answers (1)
ajith
on 11 Sep 2012
% Tschebyscheff and Equi-interval method for interpolation program % Function f(x)=1/(25*x^2)
clear all; %xe equi-interval %xc tschebyscheff x=-1:0.01:1; N=length(x); num = [5 9 13];
for i=1:3 xe = -1:2/(num(i)-1):1; for k=1:num(i) fe(k) = 1 / ( 1 + 25 * xe(k) * xe(k) ); % equi-interval function xc(k) = cos((2*(k-1)+1)*pi/(2*num(i))); % tschebyscheff point fc(k) = 1 / ( 1 + 25 * xc(k) * xc(k) ); % tschebyscheff function end for n=1:N
f(n) = 1 / ( 1 + 25 * x(n) * x(n) ) ;
pe(i,n)=0.0;
pc(i,n)=0.0;
for jj=1:num(i)
l1(jj)=1.0;
l2(jj)=1.0;
% Lagrange interpolation
for ii=1:num(i)
if ii ~= jj
l1(jj) = ( l1(jj) * ( x(n) - xe(ii) ) ) / ( xe(jj) - xe(ii) );
l2(jj) = ( l2(jj) * ( x(n) - xc(ii) ) ) / ( xc(jj) - xc(ii) );
end
end
pe(i,n) = pe(i,n) + fe(jj) * l1(jj);
pc(i,n) = pc(i,n) + fc(jj) * l2(jj);
end
if i ==1
pe4(n) = pe(1,n);
pc4(n) = pc(1,n);
elseif i==2
pe8(n) = pe(2,n);
pc8(n) = pc(2,n);
else
pe12(n) = pe(3,n);
pc12(n) = pc(3,n);
end
end
end
% graph plot figure(1); plot(x,f,x,pe4,x,pe8,x,pe12); legend('F(x)','P4(x)','P8(x)','P12(x)'); AXIS([-1 1 -3.7 1.5]) xlabel('x'); ylabel('Function F(x) and Pn(x)'); title(['interpolated graph by equi-interval method F(x) ‚Æ Pn(x)']);
figure(2); plot(x,f,x,pc4,x,pc8,x,pc12); legend('F(x)','P4(x)','P8(x)','P12(x)'); AXIS([-1 1 -0.2 1.1]) xlabel('x'); ylabel('Function F(x) and Pn(x)'); title(['interpolated graph by tschebyscheff point F(x) ‚Æ Pn(x)']);
2 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!