How to graph a piecewise function NOT using if-else statements

Good morning,
My professor is asking is to graph a piecewise function in R3 using anonymous functions and logicals and NOT using if-else statements. He also is asking to use "meshgrid" and the "surf" command. I attached the picture below. How would I start this? Thanks in advance

Respuestas (1)

Start by using meshgrid and surf with a non-piecewise function: find out how to draw a plane at all.
Then use this method:
t = linspace(0, 2*pi, 100);
y = sin(t) .* (t < pi) + cos(t) .* (t >= pi);
plot(t, y)
This is a "logical mask" to create a piecewise function. It works in 2D also.

3 comentarios

Thank you Jan. So I ended up with this:
a = @(x,y)( (x+y).*(x>=0 && y>0) + (x+y.^2).*(x>=0 && y<0) + (x.^2+y).*(x<0 && y>=0) + (x.^2 + y.^2).*(x<0 && y<0));
x = linspace(-8,8,40);
y = linspace(-10,10,50);
[x y] = meshgrid(x,y)
z = a(x,y);
surf(x,y,z)
xlabel('x');
ylabel('y');
zlabel('z');
title('Graph of a Piecewise Function');
The only problem I have is defining the truth values. When I try running the code it says, "Operands to the "||" and && operators must be convertible to logical scalar values." What does this mean? I understand plotting piecewise functions by multiplying by one truth value, but how would I go about it with two truth values? Thanks
Use & instead of &&.
Thank you!

Iniciar sesión para comentar.

Categorías

Más información sobre Line Plots en Centro de ayuda y File Exchange.

Preguntada:

el 12 de Mayo de 2018

Comentada:

el 13 de Mayo de 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by