How to plot using surf gird in 2D using function value

955 views Asked by At

if the function F is available it is easy to draw surf plot i.e.

x=1:0.1:4;
y=1:0.1:4;
[X,Y]=meshgrid(x,y);
Z=sin(X).^2+cos(Y).^2;
surf(X,Y,Z);
view(2) ;

enter image description here

in my case I calculated F function using least square: for example I have x and y vectors

x = [0    9.8312   77.1256  117.9810   99.9979];
y = [0    2754.5    4043.3    5376.3    5050.4];

the linear function of these two vector is define by

F= [1149.73 , 37.63];

therefore the estimation is equal to

z= [ones(5,1) x']*F';

which is

z = [1149.73    1519.67 4051.96 5589.35 4912.65];

and if it is plotted

plot(x,y,'b.');
hold on;plot(x,y,'b-');
hold on; plot(x,z,'-r');

enter image description here

The linear z ( red line) is showing correctly. Now I want to plot it for all possible combination of x and y using grid and I need to have a mesh for all inputs [X,Y] = meshgrid(x,y);

but how to make the Z matrix to show the intensity plot of function Z? The Z suppose to have high intensity close to z value and less value far from it. I should suppose to get something like this

enter image description here

Thanks

P.S: the F is calculated using pinv (least square).

1

There are 1 answers

4
freude On

You have to interpolate the scattered data to plot it on grid. Here is a simple example for your x, y and z vectors

  xi=linspace(min(x),max(x),100)
  yi=linspace(min(y),max(y),100)
  [XI YI]=meshgrid(xi,yi);
  ZI = griddata(x,y,z,XI,YI);
  contourf(XI,YI,ZI)