problems with TriScatteredInterp - matlab

I have two sets of scattered data x y z and x2 y2 z2
The following code should produce two overlapping surface plots
F = TriScatteredInterp(x,y,z);
z2i=F(x2,y2);
tri = delaunay(x,y);
plot = trisurf(tri,x2,y2,z2,'edgeColor','blue','FaceColor','blue','FaceAlpha',.5);
hold on
trisurf(tri,x2,y2,z2i,'edgeColor','red','FaceColor','red','FaceAlpha',.5);
Somehow, the two plots are not even close. Does anyone know how this is possible?

Since you are moving from first set of x and y to the second set x2 and y2, calculate triangulation based on x2 and y2.
tri = delaunay(x2,y2);
Don't forget hold off at the end.

Related

How to make a 2D contour plot with given data point in Octave/MATLAB?

I have a matrix whose three columns correspond to x, y and f values. I want to make a contour plot of f(x,y) in the x,y plane from these data with Octave/MATLAB.
Let's say, the matrix M is
x1 y1 f1
x2 y2 f2
x3 y3 f3
. . .
. . .
I found the function contourf requires f to be a matrix (whereas I have a vector with corresponding points).
How to generate this plot?
The x, y, and z variables that you pass to contourf are all matrices of the same size. For every point you need an x, y, and z value. You can use meshgrid to make matrices that have all the combinations of x and y values.
This example is from the doc for contourf. I added some comments to explain what is happening
% Create a vector of x values
x = linspace(-2*pi,2*pi);
% Create a vector of y values
y = linspace(0,4*pi);
% Make matrices with all combinations of x and y values for plotting
[X,Y] = meshgrid(x,y);
Z = sin(X)+cos(Y);
contourf(X,Y,Z)
This is the result of the above code

Stacking multiple solutions into a single 3D plot

y1=tanh(3*x+5*t);
y2=5*cos(x+3*t);
y3=exp(-x)*sin(2*x+t);
How can I plot y1, y2 and y3 into a single 3D plot for the fixed value of t=0.5?
Assuming yn and x are arrays of values and have been evaluated with t=0.5, the following code will plot all 3 functions on one figure:
figure
plot(x,y1,x,y2,x,y3)
I'm guessing you are additionally asking how to evaluate yn for a range of x. Simply define yn as an array and loop over all values of x:
y1 = [];
x = [1 5 8 17];
t=0.5;
for xi = x
y1 = [y1 ; tanh(3*xi+5*t)];
end

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

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) ;
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');
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
Thanks
P.S: the F is calculated using pinv (least square).
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)

Finding the equation of a contour in Matlab

I have a contour plot generated by Matlab. I wish to know the equation of one of the contours drawn by contour function.
Is there a simple way to do this?
Thanks.
P.S. I am new here, so the site didn't let me attach an image file saying I need enough reputation to post images!
From doc contour:
[C,H] = CONTOUR(...) returns contour matrix C as described in CONTOURC
and a handle H to a contourgroup object. This handle can be used as
input to CLABEL.
And from contourc:
The contour matrix C is a two row matrix of contour lines. Each
contiguous drawing segment contains the value of the contour,
the number of (x,y) drawing pairs, and the pairs themselves.
The segments are appended end-to-end as
C = [level1 x1 x2 x3 ... level2 x2 x2 x3 ...;
pairs1 y1 y2 y3 ... pairs2 y2 y2 y3 ...]

Two Gaussian distribution plot on the same axis

I am trying to plot two Gaussian distribution both with mean zero, one with variance 1 and the other with variance 2 on the same axis. Here is my code.
X= 0 + 1.*randn(2,500);
plot(X(1,:),X(2,:),'x');
hold on
%plot(m(1),m(2),'r*')
Y= 0 + 2.*randn(2,500);
plot(Y(1,:),Y(2,:),'gx')
Please check and see if i have done it correctly. I also want to have them in 2-D plot and superimpose.
Thanks.
You are plotting the data you have randomly generated (with a normal distribution). If that's what you want, yes, it works ok.
If you want to plot the density functions of the variables, you can do it the following way:
mu = 0;
sigma1 = 1;
sigma2=2;
x = -4*sigma2:1e-3:4*sigma2;
y1 = pdf('normal', x, mu, sigma1);
y2 = pdf('normal', x, mu, sigma2);
plot(x, y1)
hold on
plot(x, y2, 'r')
legend('mu=0, sigma=1', 'mu=0, sigma=2')
title('Density functions')