How do I display only certain numbers from a plot?

7 次查看(过去 30 天)
This is for a begineer Matlab class assignment so bear with me here. The assignment is to plot a graph of a first order equation, then based on the persison set by the user, it needs to tell you which x values y is equal to zero at. The problem I am having is that I get an error with my if statement and I dont know how to fix it. All I need it to do is check if there are y values between the set persison and then display the x value at that point.
clc
clear
a=input('What is the value of a? ');
b=input('What is the value of b? ');
r=input('What is the range? ');
s=input('What is the step-size for range of x ');
p=input('What is the persison of zero? ');
x=-r:s:r;
y=(a*x)+b;
plot(x,y,'r*')
if -p<y && y<p
fprintf('Y=0 at x=%f',y)
else
disp('Error. Enter a new range or persison')
end
  5 个评论
Benjamin
Benjamin 2023-4-17
This is what happens when I run my code.
What is the value of a? 1
What is the value of b? 0
What is the range? 10
What is the step-size for range of x 0.1
What is the persison of zero? 0.5
Operands to the logical AND (&&) and OR (||) operators must be
convertible to logical scalar values. Use the ANY or ALL
functions to reduce operands to logical scalar values.
Error in Project2_Practice (line 11)
if -p<y && y<p
Cris LaPierre
Cris LaPierre 2023-4-18
a=1;
b=0;
r=10;
s=0.1;
p=0.5;
x=-r:s:r;
y=(a*x)+b;
plot(x,y,'r*')
if -p<y && y<p
fprintf('Y=0 at x=%f',y)
else
disp('Error. Enter a new range or persison')
end
Operands to the logical AND (&&) and OR (||) operators must be convertible to logical scalar values. Use the ANY or ALL functions to reduce operands to logical scalar values.

请先登录,再进行评论。

回答(2 个)

Dyuman Joshi
Dyuman Joshi 2023-4-18
移动:Walter Roberson 2023-4-18
"&&" can only be used when then result of each logical expression is a scalar (as the error states as well), in all other cases you need to use a single AND "&" operator.
It's not clear if you want to compare each values of y seperately or all together? In case you want to compare all values, you need to use all() (again stated in the error). Here you can use &&, as the output of these expressions will be a scalar
if all(-p<y) && all(y<p)

Walter Roberson
Walter Roberson 2023-4-18
Your y is a vector. You want to find locations in that vector that are between -p and +p .
mask = -p < y & y < p;
Now you have a few different possibilities to consider:
  • all entries in mask might be false -- ~any(mask) . In this case either there were no roots or the step size is too large or the precision is too small
  • exactly one entry in mask might be true -- nnz(mask) == 1. In this case you have successfully found one root at x(mask)
  • there might be one grouping in mask where several entries in a row are true. This can happen if the precision is too large and the function is not changing faster than the precision would suggest
  • there might be several distinct places in mask where exactly one entry is true. This would typically correspond to multiple roots of the equation
  • you might have false roots. For example x.^2 + 1e-7 has no true roots over reals, but if your precision were 1e-6 then you would think that you found a root,

类别

Help CenterFile Exchange 中查找有关 Programming Utilities 的更多信息

标签

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by