MATLAB Answers

Problem with writing this function

3 ビュー (過去 30 日間)
Hanna Sundling
Hanna Sundling 2019 年 10 月 9 日
コメント済み: Jon 2019 年 10 月 9 日
I do not understand what is wrong with this function. When I try to run the function written like this the error down below comes up.
y = (x.-1)/((x.^2).+x)

  0 件のコメント

サインイン to comment.

採用された回答

Jon
Jon 2019 年 10 月 9 日
編集済み: Jon 2019 年 10 月 9 日
You may have some other issues also, but assuming that you have defined x as a column or row vector and you are expecting to calculate y for each element of x you should use something like
y = (x -1)./(x.^2 + x)
Note that the . operator is needed to get element by element division as in ./ and element by element exponentiation as in x.^2
The . operator is not needed and in fact is not a recognized operation for addition and subtraction which is always performed element by element in MATLAB

  0 件のコメント

サインイン to comment.

その他の回答 (1 件)

Vikas Kumar
Vikas Kumar 2019 年 10 月 9 日
Agree with Jon. I think you are getting error because of . operator you use with the addition and subtraction. This . operator is only meaningful when you want to do element by element operation, which in case of addition and subtraction is by default.
So:
y = (x-1)./((x.^2)+x)

  1 件のコメント

Jon
Jon 2019 年 10 月 9 日
Note also that parenthesis around exponentiation are not necessary either. They do not cause an error, but make the expression harder to read.

サインイン to comment.

サインイン してこの質問に回答します。


Translated by