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
조회 수: 1 (최근 30일)
이전 댓글 표시
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
채택된 답변
Paul
2021년 10월 15일
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.
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!