Plot y^2 = x^2 + 1 (The expression to the left of the equals sign is not a valid target for an assignment.)

7 Ansichten (letzte 30 Tage)
%Clear memory
clear;
%Number of points
N = 10000;
%Preallocate memory
x = zeros(1,N);
y1 = zeros(1,N);
x = -5 + (5+5)*rand(1,N);
y1^2 = x^2 + 1;
plot(x,y1), grid on;
I get the following error:
The expression to the left of the equals sign is not a valid target for an assignment.
How can I plot this first on x,y axis, then on x^2 and y^2?
Thanks in advance.

Akzeptierte Antwort

MA
MA am 1 Nov. 2014
clear all
clc;
N = 10000;
x = zeros(1,N);
y1 = zeros(1,N);
y2 = zeros(1,N);
x = -5 + (5+5)*rand(1,N);
for i=1:N
y1(i)=(x(i)^2 + 1)^(1/2);
y2(i)=-(x(i)^2 + 1)^(1/2);
end
plot(x,y1)
hold on
plot(x,y2)
grid on
good luck

Weitere Antworten (4)

David Young
David Young am 1 Nov. 2014
The error message says it all: you can't assign something to y1^2. Remember that in a programming language, the "=" operator isn't like "=" in algebra. What it does is to assign the value of the expression to its right to the variable (or, in MATLAB, to the matrix elements) given by the expression on its left.
So you need a variable on the left here. How about taking the square root?
y1 = sqrt(x.^2 + 1);
That might not get you all the way (because the square root has more than one solution) but it's a start.
Two things to note. You need .^, not ^. And the preallocation step isn't necessary; in fact it just wastes time and adds complexity.

Roger Stafford
Roger Stafford am 1 Nov. 2014
Bearbeitet: Roger Stafford am 1 Nov. 2014
You can generate this hyperbola plot using hyperbolic functions as parameters:
t = linspace(-4,4,500);
x = sinh(t);
y = cosh(t);
plot(x,y,'y-')
axis equal

Jaime
Jaime am 2 Nov. 2014
ezplot('-1*x^2 + 1*y^2 - 1')

Matt J
Matt J am 1 Nov. 2014
Bearbeitet: Matt J am 1 Nov. 2014
You cannot have mathematical expressions on the left hand side of an assignment, as you have in
y1^2 = x^2 + 1;
Presumably you meant to do
y1 = sqrt(x.^2 + 1);
or
y1=-sqrt(x.^2 + 1);

Kategorien

Mehr zu Graphics Performance finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by