When putting a variety of values in one at a time to an equation it outputs the right answer, but when I put in an array of values the answers are wrong
2 views (last 30 days)
Show older comments
Francesca Fernandes
on 15 Oct 2021
Commented: Francesca Fernandes
on 15 Oct 2021
I have an equation and a range of values I want to be input into said equation to then make a graph of. The output however gives me a range of y values that are the wrong answers and I can't work out why, but if I equate x to a single number I get the right output. The code looks like as follows:
x = -10:1:10
y = (cos(x)+tanh(x))/x+(x/100)
0 Comments
Accepted Answer
Paul
on 15 Oct 2021
If you want y to be evaluated for each element of x, you need to use the "element-wise" operator ./ (note the dot) instead of /.
x = -10:1:10
y = (cos(x)+tanh(x)) ./ x+(x ./ 100)
Simlarly you'd need .* and .^ for element-wise muliplication and exponentiation. The bare / operator is called "mrdivide," which performs a matrix division. Start reading here and follow the documentation to learn more.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!