??? Error using ==> times Matrix dimensions must agree.

Happy New year to everyone. When I am executing the below given program, I got the error message " ??? Error using ==>times Matrix dimensions must agree." Please check the problem and help me to resolve the issue,
for p1=4:1:12
E1 = sqrt((p1^.2)-(Mc^.2));
f1 = 1./(exp(E1/T) + 1);
for p2=4:1:12
E2 = sqrt((p2^.2)-(Mc^.2));
f2 = 1./(exp(E2/T) + 1);
v_rel = sqrt(((p1^.2).*(p2.^2) - (Mc.^4))/((p1^.2).*(p2.^2) + (Mc.^2).*((p1^.2) + (p2.^2) + (Mc.^4))))
end
dp12 = p2(2:end) - p2(1:end-1);
Lambda_f_Jpsi1 = sum(dp12.*p2.*p2.*f2.*v_rel.*sigma_f_Jpsi)
Lambda_f_Jpsip1 = sum(dp12.*p2.*p2.*f2.*v_rel.*sigma_f_Jpsip)
Lambda_f_ChiC1 = sum(dp12.*p2.*p2.*f2.*v_rel.*sigma_f_Chic)
end
dp11 = p1(2:end) - p1(1:end-1);
Lambda_f_Jpsi = (1./(pi.^4)).*sum(dp11.*p1.*p1.*f1.*Lambda_f_Jpsi1)
Lambda_f_Jpsip = (1./(pi.^4)).*sum(dp11.*p1.*p1.*f1.*Lambda_f_Jpsip1)
Lambda_f_ChiC = (1./(pi.^4)).*sum(dp11.*p1.*p1.*f1.*Lambda_f_ChiC1)
Please Help me. Thank you.

Antworten (2)

Image Analyst
Image Analyst am 1 Jan. 2015

0 Stimmen

Do you really mean to raise p1 and p2 to the 0.2 power, like p1^.2? Or did you mean p1.^2? Actualy p1 and p2 are scalars so you don't even need the dot unless you want p1 ^ (0.2).
What is the value of Mc and T? Are they arrays?
Anyway, please post the complete error text - ALL THE RED TEXT, not just a small snippet of it like you did. I don't know what line it's complaining about.

4 Kommentare

Captain Singh
Captain Singh am 1 Jan. 2015
Bearbeitet: Image Analyst am 1 Jan. 2015
Actually, I wanted p1 and p2 to power 2, and thanks to you for pointing the error, I've made that correction. Here Mc and T are two constant. The complete error message is:
??? Error using ==> times Matrix dimensions must agree.
Error in ==>
LambdaD_total at 267
Lambda_f_Jpsi1 =
sum(dp12.*p2.*p2.*f2.*v_rel.*sigma_f_Jpsi)
Thanks for comment.
Image Analyst
Image Analyst am 1 Jan. 2015
Bearbeitet: Image Analyst am 1 Jan. 2015
It appears you don't know how to use the debugger, which you can fix by looking at this. So if you don't use the debugger, you'll have to fix it by putting in code to do what you could do much easier in the debugger. So, put in these lines before line 267 and see what it says.
whos dp12
whos p2
whos f2
whos v_rel
whos sigma_f_Jpsi
By the way, you'll probably find that one of those is not a scalar but a matrix.
[EDIT] It looks like sigma_f_Jpsi is a structure - is that right?
yes, after putting these line before the line no. 267, it has given :
Name Size Bytes Class Attributes
dp12 1x0 0 double
Name Size Bytes Class Attributes
p2 1x1 8 double
Name Size Bytes Class Attributes
f2 1x1 8 double
Name Size Bytes Class Attributes
v_rel 1x1 8 double
Name Size Bytes Class Attributes
sigma_f_Jpsi 1x1000 16000 double complex
and end up with same error as earlier.
Does it worry you that the size of dp12 is 1-by-0? What about that sigma_f_Jpsi is filled with a thousand complex numbers?

Melden Sie sich an, um zu kommentieren.

Star Strider
Star Strider am 1 Jan. 2015

0 Stimmen

What line is throwing the error? Please post the full error, and the full line that is throwing it.
What’s Mc?
Unless we know what it is, we can’t run your code. If we can’t run your code, we can’t isolate the problem.

14 Kommentare

The code is very big and it is not possible to post full program over here and even executing the code will be much difficult for you, as it require too many input files which is not possible to send anyone. Thanks for comment.
Star Strider
Star Strider am 1 Jan. 2015
My pleasure.
What are the sizes of: dp12, p2, p2, f2, v_rel, sigma_f_Jpsi?
>> size(dp12)
ans =
1 0
>> size(p2)
ans =
1 1
>> size(f2)
ans =
1 1
>> size(v_rel)
ans =
1 1
>> size(sigma_f_Jpsi)
ans =
Column 1
1
Column 2
1000
These are the sizes of the variables. Thanks
My pleasure.
This doesn’t make sense:
size(dp12)
ans =
1 0
A scalar has a size of [1 1]. Your ‘dp12’ variable would seem to be the problem because of the strange size, but something is wrong. What is the value of ‘dp12’? How do you create it?
He uses
dp12 = p2(2:end) - p2(1:end);
right after the p2 loop, which does not make sense because p2 should be a scalar, not a vector, after it exits the p2 loop.
The debugger is so much more efficient and faster than debugging by back and forth discussion group postings - I wish he'd learn it because it would help him immensely. Especially since he's just given us a snippet of the code that we can't even run to debug for him. Many variables were defined before the code he gave us and we don't know what they are, or even if they're arrays or scalars. It makes it difficult to help him.
Star Strider
Star Strider am 1 Jan. 2015
Bearbeitet: Star Strider am 1 Jan. 2015
I didn’t catch the strange assignment of ‘dp12’. That certainly explains the anomalous size.
I certainly agree with you about the inadvisability of debugging as an online iterative process!
[EDIT] — ‘dp12’ is definitely the problem!
Actually, dp12 (or it can be said dp) is the unit of integration, as it is double integration with respect to dp (so I've assigned it as dp11 and dp12) and the integration limits are 4 (lower limit) and 12 (upper limit). It may help you to help me. Thanks
I would then define ‘dp12’ as:
dp12 = p2(2) - p2(1);
assuming that the elements of ‘p2’ are uniformly spaced.
But more to the point, why not just use the trapz function to do the integrations?
Huh? Since p2 is a scalar, the value it has after the p2 loop has ended is simply 12. It's not an array with values of 4,5,6,7,8,9,10,11,12. If you want that, you'll need to redefine p2 after the loop
for p2 = 4 : 12
% code
end
p2 = 4:12;
Star Strider
Star Strider am 1 Jan. 2015
You’re correct. (I’m not at my best today.)
In that situation, I would simply set ‘dp12=1’. Creating it as a (1x9) vector would still throw an error, since ‘sigma_f_Jpsi’ is (1x1000).
I've tried, this "dp12 = p2(2) - p2(1);" and after running the code I got the error message:
??? Attempted to access p2(12); index out of bounds because numel(p2)=1.
Error in ==> LambdaD_total at 266 dp12 = p2(12) - p2(4);
thanks
When I've also tried the second method,
for p2 = 4 : 12
% code
end
p2 = 4:12;
suggested by Image Analyst, now the size of dp12 has changed
>>size(dp12)
ans =
1 8
but it has given the same error as earlier.
??? Error using ==> times Matrix dimensions must agree.
Error in ==> LambdaD_total at 276 Lambda_f_Jpsi1 = sum(dp12.*p2.*p2.*f2.*v_rel
thanks for comment.
that should be okay. But what about the last term? Did you leave it off or did you change your equation? What about the complex number, sigma_f_Jpsi? It has 1000 complex numbers. You can't multiply a 1-by-8 array by a 1-by-1000 array element-by-element. We can't really help more because you did not put in any comments to help us figure out what you want to do. All we can see is what you put, which we know is wrong, but we don't know how to fix it because you've given us no guidance (like in the form of comments) as to what you really want to do.
Thanks for comment, that problem has been resolved.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 1 Jan. 2015

Kommentiert:

am 6 Jan. 2015

Community Treasure Hunt

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

Start Hunting!

Translated by