How do I display only certain numbers from a plot?

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 件のコメント

Image Analyst
Image Analyst 2023 年 4 月 17 日
What does "persison" mean?
Benjamin
Benjamin 2023 年 4 月 17 日
編集済み: Benjamin 2023 年 4 月 17 日
Sorry *precision
I typed this project up quick and I'm not great at spelling sorry
Cris LaPierre
Cris LaPierre 2023 年 4 月 17 日
What error message are you getting? Please share the entire message (all the red text).
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 日

0 投票

"&&" 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 日

0 投票

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,

カテゴリ

ヘルプ センター および File ExchangeProgramming Utilities についてさらに検索

製品

リリース

R2022b

タグ

質問済み:

2023 年 4 月 17 日

コメント済み:

2023 年 4 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by