How to graph values of a function above and below 0 as separate functions?

10 vues (au cours des 30 derniers jours)
Konrad Krusche
Konrad Krusche le 28 Mar 2020
Commenté : Konrad Krusche le 29 Mar 2020
I need to create a graph of y=sin(x). Where y is greater than zero, have a separate function of y^2, and where y is less than zero, have a function y^3. These should all exist on the same plot.
I think I have to define y2 and y3 ouside of the if statement. If I set y2=y; ouside 'if', I have y throughout the whole graph. Either way, the if statement doesn't seem to work.
And on top of that, I don't know how to graph this, because x and y2 will be different lengths if y2 only exists where y>0
Here is what I came up with, but it totally doesn't work. The photo included is a more complex function, but the idea is exactly the same. I'm supposed to use if and elseif. Thank you! Stay safe and healthy everyone!
t= 0:0.2:3*pi;
y=sin(t);
figure(1)
plot(t,y,'-.b')
grid on
hold on
if y>0
y2=y.^2;
elseif y<0;
y3=y.^3;
end
figure(1)
plot(t,y2,'om')
figure(1)
plot(t,y3,'dk')

Réponses (4)

Adam Danz
Adam Danz le 28 Mar 2020
The conditional statements in your code aren't doing what you think they are doing. They return true only when the entire vector meets the criteria. Instead, use indexing.
x = 0 : pi/12 : 4*pi;
y = sin(x);
y2(y>0) = y(y>0).^2;
y2(y<0) = y(y<0);
y3(y<0) = y(y<0).^3;
y3(y>0) = y(y>0);
hold on
plot(y, '-b', 'LineWidth', 4)
plot(y2, '--m', 'LineWidth', 2)
plot(y3, '--c', 'LineWidth', 2)
  1 commentaire
Konrad Krusche
Konrad Krusche le 28 Mar 2020
Thank you. I knew that something about the if wasn't working because it's a vector, but didn't know exactly how it works.

Connectez-vous pour commenter.


Image Analyst
Image Analyst le 28 Mar 2020
Try this:
t = 0:0.2:3*pi;
y = sin(t);
y2 = y .^ 2;
y3 = y .^ 3;
hFig = figure;
plot(t,y,'-.b')
grid on
hold on
positiveYIndexes = y > 0;
negativeYIndexes = y < 0;
plot(t(positiveYIndexes), y2(positiveYIndexes), 'om-', 'LineWidth', 2')
plot(t(negativeYIndexes), y3(negativeYIndexes), 'dk-', 'LineWidth', 2')
xlabel('t', 'FontSize', 20);
ylabel('y', 'FontSize', 20);
legend('y', 'y squared', 'y cubed', 'Location', 'north');
hFig.WindowState = 'maximized';

Star Strider
Star Strider le 28 Mar 2020
Subscript the elements and provide corresponding time vectors for each one:
t= 0:0.2:3*pi;
y=sin(t);
for k = 1:numel(t)
if y(k)>0
t2(k) = t(k);
y2(k)=y(k).^2;
elseif y(k)<0
t3(k) = t(k);
y3(k)=y(k).^3;
end
end
figure(1)
plot(t,y,'-.b')
grid on
hold on
figure(1)
plot(t2,y2,'om')
figure(1)
plot(t3,y3,'dk')

Image Analyst
Image Analyst le 28 Mar 2020
If you absolutely MUST do it with an if/else construct, and throw away most of the benefits of the vectorization capabilities of MATLAB, then you can do this:
% Initialization steps.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 15;
t= 0:0.2:3*pi;
y=sin(t);
hFig = figure;
plot(t,y,'-.b')
grid on
hold on
y2 = nan(1, length(y)); % Nan points will not get plotted.
y3 = nan(1, length(y));
for k = 1 : length(y)
if y(k) >= 0
y2(k) = y(k) ^ 2;
else
y3(k) = y(k) ^ 3;
end
end
plot(t, y2, 'om-', 'LineWidth', 2')
plot(t, y3, 'dk-', 'LineWidth', 2')
xlabel('t', 'FontSize', 20);
ylabel('y', 'FontSize', 20);
legend('y', 'y squared', 'y cubed', 'Location', 'north');
hFig.WindowState = 'maximized';
  3 commentaires
Image Analyst
Image Analyst le 29 Mar 2020
So how are you going to do it now? You didn't tag this as homework, which means (if you had) we'd have given you hints on how to do it, not total solutions. If you turn in our solutions as your own work, and your professor finds out, you could be in serious trouble.
Konrad Krusche
Konrad Krusche le 29 Mar 2020
My bad, I should have tagged it appropriately. He sent us additional materials on how to use if (which I only saw after this post), and the only main difference from what you posted above, is he defined y2 as an empty vector (y2=[]). I of course posted a different function and variables here, as it was not my intention to copy. The only part of my solution that is the same as above is the definition of y2, as a vector of non-numbers. I should probably inform him that I asked for help. Again, thank you, and I apologize for my mistake, I'll make sure to tag correctly if I ask for help for again.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Environment and Settings dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by