How to plot x=y2

22 views (last 30 days)
Ahmet Tuter
Ahmet Tuter on 8 Nov 2017
Commented: DGM on 21 Feb 2025
Hello I am just starting out. How do I plot x = y^2 please?
  1 Comment
John D'Errico
John D'Errico on 8 Nov 2017
Edited: John D'Errico on 8 Nov 2017
Start out by reading the getting started tutorials. If you will ask basic questions about every possible thing, this will get old quickly.
If you have x=y^2, you could use a tool like ezplot. Or fimplicit.
syms x y
fimplicit(x-y^2)
Or, you could recognize that y=+/- sqrt(x), and then just use plot.

Sign in to comment.

Answers (4)

M
M on 8 Nov 2017
y = 0:1:100; % define y as you want
x = y^2;
plot(y,x) % plot X versus Y
  3 Comments
Harsh Singh
Harsh Singh on 27 Feb 2022
y = 0:1:100; % define y as you want
x = y.^2; % do an element wise operation
plot(y,x) % plot X versus Y
John D'Errico
John D'Errico on 27 Feb 2022
Note that if you form x == y.^2 in this way, you will see ONLY one arc of the sideways parabola, missing the negative branch comletely.

Sign in to comment.


John D'Errico
John D'Errico on 27 Feb 2022
Edited: John D'Errico on 27 Feb 2022
Has nobody yet fully answered this question? Since it is now long past the time when a homework answer would have done homework for the student, I'll post several solutions. The simple way to plot that relationship is to use fimplicit. For example:
F = @(x,y) x - y.^2;
fimplicit(F)
xlabel X
grid on
ylabel Y
I could have used fimplicit with a syms relationship too.
syms x y
eqn = x - y.^2; % the relationship is implicitly set to zero for the plot.
fimplicit(eqn)
xlabel X
grid on
ylabel Y
As you can see, fimplicit (in either case) plots both branches of the sideways parabola. You can control how fr out the plot goes using the argiments to fimplicit.
Other automatic tools like ezplot, or fplot should also work.
If you really want to to the work yourself, then you need to understand the nature of this function, with two branches. So you might do this instead:
Fxofy = @(y) y.^2; % This function computes x, as a function of y. Remember x=y^2
% Now we can plot the relationship, as
y = linspace(-3,3,1000);
x = Fxofy(y);
plot(x,y,'-r')
xlabel X
grid on
ylabel Y
You can see this neatly sidesteps the problem of there being two branches of the function to deal with.
Finally, could I have plotted this relationship in another way? Of course.
x = linspace(0,10,500)'; % specifically a column vector x
Fyofx = @(x) [sqrt(x), -sqrt(x)]; % this returns BOTH branches, as columns of the result
y = Fyofx(x);
plot(x,y,'-')
xlabel X
grid on
ylabel Y
Here we see both branches plotted as distinct colors, the positive branch in blue, and the negative branch in red.

michael
michael on 21 Feb 2024
Edited: michael on 21 Feb 2024
muchachos apoyen esto como es ?
(x--y)^2 como seria esta funcion

KAREN ROSARIO
KAREN ROSARIO on 21 Feb 2025
y(x)=x^2
function outpt=karen(x)
%esta funcion debe calcular x^2
output=x*x;
  1 Comment
DGM
DGM on 21 Feb 2025
For scalar x, karen(x) will calculate the square of x. However, there are a few issues with this answer.
  • It should be clarified that the line "y(x)=x^2" is a descriptive statement, not part of the code.
  • The function karen(x) will throw an error if x is a vector, which limits its usefulness.
  • The output of the function is misspelled, so it will never return any result anyway.
  • The question is about how to plot the inverse function.
I'll add that karen() is the sort of nondescriptive function name that should be avoided. Also, I'm going to argue that the function should be closed with an end statement. As a local function in a script, it is necessary. There are contexts where it's not necessary, but I can see no advantage to adopting an inconsistent practice that only degrades readability.
John's answer is a good reference, and M's answer is very simple and works fine. The given function can be fixed and applied easily.
% plot y = x^2 over x = [-1 1]
x = linspace(-1,1,100);
y = mysquare(x);
plot(x,y)
grid on
% plot x = y^2 over y = [-1 1]
y = linspace(-1,1,100);
x = mysquare(y);
plot(x,y)
grid on
% this is probably a bit unnecessary for such a simple function, but ...
function output = mysquare(x) % fix the output typo, use a good variable name
% Y = MYSQUARE(X)
% esta funcion debe calcular x^2
%
% X is a numeric scalar or array
% Y is the elementwise square of X
%
output = x.^2; % elementwise!
end

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!