Clear Filters
Clear Filters

problems writing a function

1 view (last 30 days)
linofex
linofex on 25 Feb 2015
Edited: linofex on 27 Feb 2015
Hi all, I'm trying to write a function, but I have some problems, this is the script:
exp((abs((x+2)*(x-3))/((x+3)*(x-2))))
But Matlab shows me an error:
Error using *
Inner matrix dimensions must agree.
So I changed all with .*( what is the difference? ) The plot shows me a straight line at 2,42 (as if I only copy the function on matlab command window), but If I copy the function on wolfram site, the result is as I expect. Why? Thanks in advance, Alessandro
  1 Comment
Stephen23
Stephen23 on 25 Feb 2015
Edited: Stephen23 on 25 Feb 2015
The difference is simple:
  • without the period follow the rules of linear algebra, as used in mathematics using matrices, for example * performs matrix multiplication.
  • with the period performs element-wise operations on the corresponding elements of the arrays, for example .* gives the Hadamard product.
This is clearly explained in the documentation:

Sign in to comment.

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 25 Feb 2015
exp((abs((x+2).*(x-3))./((x+3).*(x-2))))
  5 Comments
Stephen23
Stephen23 on 26 Feb 2015
Edited: Stephen23 on 27 Feb 2015
"when I work with parenthesis I must write .*"
No, because the difference between * and .* has nothing to do with the parentheses at all. Read the documentation to know why: both of these operations can be performed on matrices, so the fact that you create matrices with parentheses is completely irrelevant to which operation you use. The difference is in what they calculate.
linofex
linofex on 27 Feb 2015
Edited: linofex on 27 Feb 2015
thank you for the documentation,solved the difference between * , or .* but why in my function, without matrices, there was the error?

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!