Working with meshgrid in MATLAB - 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);

Related

How can I plot a surface generated by a function f(x,y) where the values x and y are subject to constraints?

I'm new to Matlab and I have a function
and
How can I plot:
Define your X and Y in a linear array
X = linspace(-2, 2, 1000);
Y = linspace(-2, 2, 1000);
Mesh them so you have a grid of x and y
[x,y] = meshgrid(X,Y);
Get the value for your function
f = sqrt(x.^2 + y.^2);
Define your domain
D = (1 <= x.^2 + y.^2);
Set everything outside your domain to nan's so it won't plot
f(~D) = nan;
Plot the surface
surf(x,y,f, 'linestyle', 'none')

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

Is there a way to graphically add two curves in MatLab?

Lets say I have a sine curve and a cosine curve. Is there any way to add them graphically? Rather than doing sin + cos?
I'm plotting things that are functions of different variables (lets say one curve takes x-values from an array of integers and the other from an array of irrational numbers), so I don't know how else to add them.
Let's say you want to plot y1 = f1(x1) and y2 = f2(x2) you could do this
X = union(x1, x2);
Y1 = interp1(x1, y1, X);
Y2 = interp1(x2, y2, X);
Y = Y1 + Y2;
plot(X, Y)
So to see this in action
x1 = 2 * pi * rand(50, 1);
x2 = 2 * pi * rand(50, 1);
y1 = cos(x1).^2;
y2 = sin(x2).^2;
X = union(x1, x2);
Y1 = interp1(x1, y1, X);
Y2 = interp1(x2, y2, X);
Y = Y1 + Y2;
plot(X, Y)

Plotting a 3d Function using Meshgrid and Surfc

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)

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);