Interpolation of given data in 2d domain
2 views (last 30 days)
Show older comments
x=0,4,6,9,11,17,20
y=0,2,3,7,10,15
x is (7,1) vector & y is (6,1)
z will have (known data) 7*6=42 values in this domain.
how to interpolate z to some new x & y. let say now x(5,1)=0,5,10,15,20 & y(6,1)= 0,3,6,9,12,15.
I am using "griddata" bit getting very lil error. please help!
0 Comments
Answers (1)
Davide Masiello
on 5 Apr 2023
Example
xdata = linspace(1,10,10)
ydata = linspace(0,1,11)
zdata = rand(length(ydata),length(xdata))
x0 = [2.5 3.5 4.5 5.5];
y0 = [0.25 0.35 0.45 0.55];
z0 = interp2(xdata,ydata,zdata,x0,y0)
2 Comments
Davide Masiello
on 6 Apr 2023
Edited: Davide Masiello
on 6 Apr 2023
xdata = linspace(1,10,20);
ydata = linspace(0,20,30);
[xgrid,ygrid] = meshgrid(xdata,ydata);
zdata = sin(xgrid)+cos(ygrid);
figure(1)
surf(xgrid,ygrid,zdata)
Now, if you want data at a certain fixed x0 and variable y, you can do
x0 = 6.5;
z0 = interp2(xdata,ydata,zdata,x0,ydata);
You can visualise this
figure(2)
subplot(2,1,1)
surf(xgrid,ygrid,zdata),hold on
plot3(x0*ones(size(ydata)),ydata,z0,'r','LineWidth',4)
subplot(2,1,2)
plot(ydata,z0)
xlabel('y')
ylabel('z')
legend('x0 = 6.5')
Same thing for fixed y0
y0 = 5.66;
z0 = interp2(xdata,ydata,zdata,xdata,y0);
figure(3)
subplot(2,1,1)
surf(xgrid,ygrid,zdata),hold on
plot3(xdata,y0*ones(size(xdata)),z0,'r','LineWidth',4)
subplot(2,1,2)
plot(xdata,z0)
xlabel('y')
ylabel('z')
legend('y0 = 5.66')
In general, you can extrapolate any path on the surface of z for a given function y(x).
x0 = xdata;
y0 = -2*x0+18;
z0 = interp2(xdata,ydata,zdata,x0,y0);
figure(4)
subplot(2,1,1)
surf(xgrid,ygrid,zdata),hold on
plot3(x0,y0,z0,'r','LineWidth',4)
subplot(2,1,2)
plot3(x0,y0,z0,'k','LineWidth',2)
xlabel('x')
ylabel('y')
zlabel('z')
legend('y = -2x+18')
See Also
Categories
Find more on Interpolation 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!