Plotting a 3d Function using Meshgrid and Surfc - matlab

I know this seems like a basic question—and it probably is. I am having trouble learning how to plot this function using meshgrid and surfc. I keep getting the error saying that "The surface Z must contain more than one row or column." Why am I getting this error? Here's what I have to do:
x = 0:.01:1;
y = 0:.01:1;
f = #(x, y) sin(4*pi.*x).*sin(y).*(1-y) + (1./sqrt(1+10.*(x.^2)+ 100.*(y-0.5).^2))
Z = f(x, y);
[X, Y] = meshgrid(x, y)
surfc(X, Y, Z)

Got it:
x = 0:.01:1;
y = 0:.01:1;
f = #(x, y) sin(4*pi.*x).*sin(y).*(1-y) + (1./sqrt(1+10.*(x.^2)+ 100.*(y-0.5).^2))
[X, Y] = meshgrid(x, y)
Z = f(X, Y);
size(x)
size(Y)
size(Z)
surfc(X, Y, Z)

Related

Double integral - Matlab

Trying to find double integral of that function:
Matlab code:
x = -1:0.05:1;
y = 0:0.05:1;
[x,y] = meshgrid(x);
F = sqrt((x).^2.*y) * ((sin((x).^2 .* (y).^2)).^3) - ((cos((x).^3.*(y).^3)).^5);
surfl(x,y,F);
colormap summer;
shading interp;
dblquad('sqrt((x).^2.*y) * (sin((x).^2 .* (y).^2)).^3 - (cos((x).^3.*(y).^3)).^5', -1, 1, 0, 1)
Errors:
Untitled
Error using surf (line 57)
X, Y, Z, and C cannot be complex.
Error in surfl (line 129)
h = surf(cax,x,y,z);
Error in Untitled (line 5)
surfl(x,y,F);
How can I solve these errors?
In this line:
[x,y] = meshgrid(x);
You are basically doing:
[x,y] = meshgrid(x, x);
So basically x and y both go from -1 to 1 and since the equation has a sqrt(x.^2 .* y), you will get complex numbers. To generate a mesh using x, y with the bounds you specified for x,y use:
[x,y] = meshgrid(x, y);

Plotting four variables u = f(x, y, z) with ranges of x, y, z in Matlab

I'm trying to plot four variables (x, y, z, u) in a 3D space.
Below I defined u = f(x,y,z) = (x-y)/z and ranges for x, y and z.
Then I used the scatter3 function for plotting.
But the linspace command only returned
u1 = f(x1, y1, z1)
u2 = f(x2, y2, z2)
...
I wanted to plot my variables so that I would get values of u for all x, y and z, so as well as u_i = f(x_i, y_i, z_i), I want:
u112 = f(x1, y1, z2)
u121 = f(x1, y2, z1)
...
Which function do I need to use for this?
Full code:
z = linspace(1,2,50);
y = linspace(0,0.5,50);
x = linspace(-1,1,50);
f = (x-y)./z;
scatter3(x,y,z,5,f,'filled')
for i=1:50
if f(i)<0
scatter3(x,y,z,5,f,'r','filled');
else
scatter3(x,y,z,5,f,'b','filled');
end
end
You need to pass your x, y, and z vectors to meshgrid to have it generate points to fill the whole 3-D volume:
z = linspace(1, 2, 50);
y = linspace(0, 0.5, 50);
x = linspace(-1, 1, 50);
[X, Y, Z] = meshgrid(x, y, z);
f = (X-Y)./Z;
These will be 50-by-50-by-50 matrices. To plot them with scatter3 you will need to reshape them into column vectors using the colon operator:
scatter3(X(:), Y(:), Z(:), 5, f(:), 'filled');
If you'd like to plot negative values of f as red and positive values as blue, you can call scatter3 like so and add a jet colormap (no loops necessary):
scatter3(X(:), Y(:), Z(:), 5, (f(:) < 0), 'filled');
colormap(jet);

Evaluating a function over a given interval for plotting

I'm supposed to plot a certain Lagrange polynomial over a given interval [a,b] using a function that evaluates this polynomial pointwise:
function y = evalLagrangePolynomial(X, k, x)
n = numel(X);
y = 1;
for i=1:n
if i ~= k
y = y * (x - X(i)) / (X(k) - X(i));
end
end
end
However i'm a little confused how to tackle this problem; is there some native matlab function for this kind of situation?
I don't know of a native function but, here's one I wrote up.
XY = sortrows(rand(10, 2));
X = XY(:,1);
Y = XY(:,2);
x = linspace(0, 1, 100);
y = LagrangePolynomial(X, Y, x);
plot(x, y, '-', X, Y, 'o')
With the function
function y = LagrangePolynomial(X, Y, x)
if isscalar(x)
l = GenerateLagrangePolynomial(X, x)';
y = sum(l .* Y);
else
y = arrayfun(#(w) LagrangePolynomial(X, Y, w), x);
end
end
function l = GenerateLagrangePolynomial(X, x)
[X, ~] = meshgrid(X, ones(size(X)));
lPreprod = (x - X') ./ (X - X');
lPreprod(isinf(lPreprod)) = 1;
l = prod(lPreprod, 1);
end

Contour plot for 3D vector

I am trying to produce a contour plot for the 3D vectors returned by a custom function in the xy plane where z = 0.
I tried this but I just get an empty graph:
% Stand in for the real function I want to plot.
f = #(x, y, z) [x ^ 2, y ^ 2, x * y + z];
x = linspace(-5, 5, 50);
y = linspace(-5, 5, 50);
z = zeros(length(x), length(y), 3);
% I know this can be vectorized but the function I really want to graph can't
% be.
for i = 1:length(x)
for j = 1:length(y)
z(i, j, :) = f(x(i), y(j), 0);
end
end
figure;
axis equal;
contour(x, y, z);
You should mention what your axis will be. You have x,y and 3 outputs from f.
If you consider 3 outputs of your f as the ones to be plotted then you should use,
contour(z(:,:,1),z(:,:,2),z(:,:,3));
Which will give you this,
I think what you are looking for, is a function with one output, like,
f = #(x, y,z) [x ^ 2 + y ^ 2 + x * y + z ];
x = linspace(-5, 5, 50);
y = linspace(-5, 5, 50);
z = zeros(length(x), length(y));
for i = 1:length(x)
for j = 1:length(y)
z(i, j) = f(x(i), y(j),0);
end
end
contour(x,y,z,20);

Working with meshgrid in MATLAB

How would you replace the following by meshgrid?
N=.33;
P=.45;
for i =1:10
for j=1:20
x0=i+N;
y0=j+P;
f(i,j)=exp(x0+y0);
end
end
I think something like
[X, Y] = meshgrid(1:10, 1:20);
Z = exp( (X+N) + (Y+P) );
% surf(X, Y, Z);
read here
edit
as user requested (if I've understood well):
[X, Y] = meshgrid(1:10, 1:20);
X1 = X + N; Y1 = Y + P;
Z = exp( X1 + Y1 );
% surf(X1, Y1, Z);