Equation to represent mountain-with-hollow-like surface - matlab

I want to use function where x and y are coordinates forming circle, z(x, y) grows from 0 at the edge to max at the R/2 and back to 0 at the center without sharp changes. I stucked with
t = -pi:pi/180:pi;
R = 5;
x = R*cos(t);
y = R*sin(t);
for i = 1:361
for j = 1:361
z(i,j) = exp( sqrt((x(i)).^2 + (y(j)).^2));
end
end
[u, v] = meshgrid(x, y);
mesh(u, v, z), grid on;
How should I realize z to add this drop to the middle? Any suggestions appreciated!

Maybe subtract two 2D gaussians with a different standard deviation?
% the area
x = linspace(-5,5,1E2);
y = linspace(-5,5,1E2);
sig1=1;
sig2=2;
%2D gaussian
efac = 1/(2*sig1);
X = exp(-efac*x.^2);
Y = exp(-efac*y.^2)';
z1 = Y*X;
z1=z1./max(z1(:));
%2D gaussian
efac = 1/(2*sig2);
X = exp(-efac*x.^2);
Y = exp(-efac*y.^2)';
z2 = Y*X;
z2=z2./max(z2(:));
[u, v] = meshgrid(x, y);
mesh(u, v, z2-z1), grid on;
An alternative that has zero outside of R and is more along the lines of your own code:
x = linspace(-2*pi,2*pi,1E2);
y = linspace(-2*pi,2*pi,1E2);
[u, v] = meshgrid(x, y);
r = sqrt(u.^2+v.^2);
z = sin(r);
z(r>pi)=0;
mesh(u, v, z), grid on;
Or to make it a little less sharp at the base:
z = sin(r).^2;

Related

How to plot this cake piece like surface in matlab?

I am trying to plot a cake piece like surface in matlab but I have no idea how to define z to make it look like a cake pie. The shape I need is this:
This is the code I wrote so far:
th = linspace(0, pi/3);
r = linspace(0, pi/3);
% z = linspace(0, 10);
[R, TH] = meshgrid(r, th);
x = R.*cos(TH);
y = R.*sin(TH);
z = R;
% z = 10 * ones(size(x));
ss = surf(x, y, z, 'FaceAlpha',0.3);
ss.EdgeAlpha = 0.6;
ss.FaceAlpha = 0.1;
it will look similar to that shape, but not exactly, I mean the shape will be rough:
z = rand(size(x));
another option with smoother surface:
z = zeros(size(x));
[m,n]=size(x);
for i=1 : n
for j=1 : m
if(mod(i+j,2)==0)
z(i,j) = 1;
end
end
end

Making parametric plot with matlab

Is it possible to plot and make my x and y axis be depended on a parameter?
For example, I'd like that my x axis will be divided to 0 1/10L 2/10L 3/10L....L
and to plot the function on that exact axis, is it possible?
This is what I tried:
x = 0:0.1*L:10*L
plot(x,func1(x))
hold on
plot(x+xShift,func2(x)+yShift)
grid on
the shifts I'm adding are just some shifts because I'd like the second function to start from a different x and y.
This should do the trick:
L = 4;
xShift = 5;
yShift = 2;
y = #(x) x .^ 2;
x = 0:(0.1*L):(10*L);
x_shifted = x + xShift;
y = y(x);
y_shifted = y + yShift;
plot(x,y)
hold on;
plot(x_shifted,y_shifted)
hold off;
grid on;
ticks = 0:(10*L) + xShift;
set(gca,'XTick',ticks);
lim = max(y_shifted);
set(gca,'YLim',[0 max(y_shifted)]);
Output:

Plotting x,y,z with intervals

How do I plot xyz In rectangular, polar, and 3-D?
for x = 0 to 35pi:
Y = x*sin(x)
Z = x*cos(x)
Using the the intervals of X which provides very smooth plots . Create three plots including tittle and labels .
This is the input I have put in so far. I'm not sure if this is correct:
x = pi*linspace(0,35);
y = pi*x,sin(pi*x);
z = pi*x,cos(pi*x);
plot3(x,y,z)
title('data analysis')
xlabel('independent x')
ylabel('dependent y')
zlabel('z')
I believe this solves the problem as you describe it:
x = linspace(0, 35*pi, 10000);
y = x .* sin(x);
z = x .* cos(x);
plot3(x, y, z);
title('data analysis');
xlabel('independent x');
ylabel('dependent y');
zlabel('z');

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