%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;
我收到以下错误:
The expression to the left of the equals sign is not a valid target for an assignment.
我如何首先在x,y轴上绘制,然后在x ^ 2和y ^ 2?
上绘制提前致谢。
答案 0 :(得分:2)
您的功能是"multi-valued"。如果你求解y1
,你将得到一个+/-平方根的解决方案。因此,您必须将您的功能分解为两个并绘制每个分支。有些人可能还建议使用ezplot
,这很方便,但不够灵活:
ezplot('y1^2 = x^2 + 1')
答案 1 :(得分:0)
另一个答案是使用ezplot提供解决方案,这是一种可接受的方式。我想扔进我的目的是纠正你的:
%Clear memory
clear;
%Number of points
N = 10000;
x = -5:0.01:5; %This creates a vector from -5 to 5 with an interval of 0.01.
y1 = sqrt(x.^2 + 1); %Since you want to solve the equation y^2=x^2+1, one of the solution is the positive root of x^2+1 and the other is the negative.
plot(x,y1); %Plots positive root
hold on;
plot(x,-y1), grid on; %Plots negative root