Matrix dimension Error while attempting to divide

Dear all,
The following is my Mat lab code:
clc
f = 0:1:100000;
c = (f.^2)*( (2.687/(10000000000000))+ ((7.858/100)/((f.^2)+1.226)) + ((1.481/10000)/((f.^2)+(1.522*(1000000)))) );
But I am getting the error
Error using /
Matrix dimensions must agree.
Error in npb (line 4)
c = (f.^2)*( (2.687/(10000000000000))+ ((7.858/100)/((f.^2)+1.226)) + ((1.481/10000)/((f.^2)+(1.522*(1000000)))) );
How can I fix this. Kindly help.

 Accepted Answer

This works without error:
c = (f.^2) .* ( (2.687/(10000000000000)) + ((7.858/100) ./ ((f.^2)+1.226)) + ((1.481/10000) ./ ((f.^2)+(1.522*(1000000)))) );

3 Comments

The formula to be implemented is as shown in the figure. Should I use ./ or .\?
The documentation clearly states the difference between rdivide ./ and ldivide .\:
" B.\A divides each element of A by the corresponding element of B."
" A./B divides each element of A by the corresponding element of B"
Or you can try it yourself:
>> 2./5
ans =
0.4
>> 2.\5
ans =
2.5
MATLAB is nice like that: you can try code out and your computer does not explode into flames!
The decision is yours, because B.\A gives the same result as A./B. Most people write the numerator first, so perhaps you should stick with that.

Sign in to comment.

More Answers (1)

You knew (for some reason) to use .^ when raising to a power. But with vectors or arrays, you also need to use .* and ./ for element-wise multiplication and division respectively.

2 Comments

Thanks John. How can I understand the difference between ./ and .\? I could not pick much from the information given on Mat lab site.
@Pramod Bharadwaj: Simply try it:
2 \ 3
2 / 3
So you see: / is the standard division.

Sign in to comment.

Asked:

on 21 Feb 2016

Commented:

Jan
on 21 Feb 2016

Community Treasure Hunt

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

Start Hunting!