Error: Inner Matrix dimensions must agree

INPUT:
mg=4000;
L=4;
r_min=1.2;
r_max=2.2;
r=1.2:0.05:2.2;
theta_min=-20;
theta_max=80;
theta=-20:5:80;
phi=0:9:180;
gamma=phi+20;
c_2=((r_max^2)-(r_min^2))./(cos(gamma+theta_min)-cos(gamma+theta_max));
c_1=(r_min^2)+c_2.*cos(gamma+theta_max);
a=0.5*(((c_1+c_2).^(1/2))+((c_1-c_2).^(1/2)));
b=c_2/(2*a);
F=(r*mg*L*cos(theta))/(b*a*sin(gamma+theta));
WORKSPACE:
phi: 1x21 double
r: 1x21 double
theta: 1x21 double
OUTPUT:
>> MatLab_Code_1
Error using *
Inner matrix dimensions must agree.
Error in MatLab_Code_1 (line 37)
F=(r*mg*L*cos(theta))/(b*a*sin(gamma+theta));

 Accepted Answer

Try using the dot operator for b and F as well
b=c_2./(2*a);
F=(r*mg*L.*cos(theta))./(b.*a.*sin(gamma+theta));

2 Comments

heu Manjo
how i can find out when it s neccessary using .* or not? or give me suggestion for more self-study about it .
it is your favor if reply me on gmail. d.physic54
An account that's been inactive for the last six years isn't likely to spring to life to answer your question.

Sign in to comment.

More Answers (1)

Ced
Ced on 22 Oct 2014
Edited: Ced on 22 Oct 2014
First things first... please try to display your question in a more accessible form.
1. Use the Code {} button to insert code
2. explain what this is about, what the question is.
3. Have you tried debugging it yourself? What have you tried? What worked/didn't?
Then, to your question: "Inner matrix dimensions must agree" means that you are applying an operator to two matrices which don't match in their number of required rows/columns. Remember that matlab always does matrix operations unless stated otherwise.
Concretely in your example:
a) you probably want to do element-wise operations, e.g. compute the force for different values of r, theta, etc. You can specify element-wise operations by adding a "." to your operator, e.g. "./" instead of "/" and ".*" instead of "*".
b) If you have a number of vectors you want to combine in a formula, it may be a good idea to use linspace(a,b,N) instead of e.g. 10:5:100. That way, you'll be sure to have the same number of elements in all vectors.

Asked:

Cat
on 22 Oct 2014

Commented:

DGM
on 9 May 2021

Community Treasure Hunt

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

Start Hunting!