Interpolation of given data in 2d domain

1 view (last 30 days)
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!

Answers (1)

Davide Masiello
Davide Masiello on 5 Apr 2023
Example
xdata = linspace(1,10,10)
xdata = 1×10
1 2 3 4 5 6 7 8 9 10
ydata = linspace(0,1,11)
ydata = 1×11
0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000 0.8000 0.9000 1.0000
zdata = rand(length(ydata),length(xdata))
zdata = 11×10
0.8112 0.0708 0.5041 0.6023 0.0818 0.1820 0.7003 0.4842 0.3335 0.6683 0.6788 0.8340 0.0349 0.5122 0.0715 0.9324 0.2119 0.2980 0.4767 0.1168 0.5219 0.1053 0.9267 0.0636 0.9447 0.8776 0.3619 0.3600 0.0824 0.3414 0.6298 0.9852 0.5891 0.6079 0.6031 0.0961 0.9830 0.2188 0.9467 0.5634 0.6200 0.2704 0.6060 0.2413 0.8322 0.6864 0.3460 0.5554 0.4096 0.2714 0.4857 0.1183 0.4792 0.5002 0.2580 0.4702 0.4455 0.6356 0.7466 0.3907 0.9321 0.8234 0.5167 0.7972 0.9827 0.5531 0.2857 0.6909 0.0056 0.7744 0.5190 0.9112 0.0090 0.8131 0.5675 0.9623 0.0170 0.9269 0.9812 0.2971 0.4493 0.6002 0.2361 0.8474 0.6592 0.4733 0.6316 0.8359 0.8067 0.9577 0.6325 0.8260 0.2111 0.7607 0.2610 0.6142 0.3501 0.8030 0.6859 0.8245
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)
z0 = 1×4
0.6516 0.5111 0.4579 0.5660
  2 Comments
ravi shukla
ravi shukla on 5 Apr 2023
how will i get z0(4,4)? similarly as old zdata.
which means z0 with x0(1) and y0(1), y0(2), y0(3) , y0(4) and similar combinations of x0(2).. x0(3)... x0(4) with all y0s
Davide Masiello
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')

Sign in to comment.

Categories

Find more on Introduction to Installation and Licensing 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!