if true
% code
for j1 = 2:length(t)
q1(j1) = (rho2(j1) - rho2(j1-1))*v2(j1-1)./((rho1_final - rho2(j1))*dt);
v2(j1) = v2(j1-1) + (q1(j1)-q2)*dt;
v1(j1) = v1(j1-1) - q1(j1)*dt;
end
end
I ended up getting this error in the second line : In an assignment A(:) = B, the number of elements in A and B must be the same. What is the mistake?

10 件のコメント

Torsten
Torsten 2018 年 7 月 12 日
What is q2 ? A scalar ? A vector ?
new user 00
new user 00 2018 年 7 月 12 日
編集済み: Image Analyst 2018 年 7 月 12 日
Its a vector. I've pre-allocated q1, v1,v2 as
q1(1:length(t)) = 0;
v1(1:length(t)) = 0;
v2(1:length(t)) = 0;
Torsten
Torsten 2018 年 7 月 12 日
If q2 is a vector, then v2(j1-1) + (q1(j1)-q2)*dt is a vector.
Now you assign this vector to v2(j1) which is a scalar.
That's not possible.
new user 00
new user 00 2018 年 7 月 12 日
Sorry I've got it wrong. q2 is a scalar. I got a little confused with q1. Also, v1 and v2 are vectors.
Torsten
Torsten 2018 年 7 月 12 日
dt is defined before and is a scalar ?
new user 00
new user 00 2018 年 7 月 12 日
Yeah it has been defined before and is scalar.
Torsten
Torsten 2018 年 7 月 12 日
Then I give up.
new user 00
new user 00 2018 年 7 月 12 日
Haha. Too early!!
Dennis
Dennis 2018 年 7 月 12 日
It is way easier to answer your question if you provide the needed information. You are probably trying to assign a vector to q1(j1),v2(j1) or v1(j1). If dt is a scalar you should check if rho1_final is a scalar und if rho2() does return scalars.
In general it helps to provide the code with possible inputs, related functions (like rho2) and the complete error message - yes, knowing in which line an error occurs does sometimes help to solvee the problem.
new user 00
new user 00 2018 年 7 月 12 日
Okay. rho_final is a scalar. rho2() is a vector which has the same number of elements as q1.

サインインしてコメントする。

回答 (2 件)

Image Analyst
Image Analyst 2018 年 7 月 12 日

0 投票

Try
v2(j1) = v2(j1-1) + (q1(j1) - q2(j1)) .* dt;

4 件のコメント

new user 00
new user 00 2018 年 7 月 12 日
I'm getting the error for q1.
Image Analyst
Image Analyst 2018 年 7 月 12 日
Then read this link so that you'll know how to create a reply that will allow people to give an answer to you.
and see this link so that you learn how to debug your code.
new user 00
new user 00 2018 年 7 月 12 日
You could have told me that the information provided in my question is insufficient instead of sending me links.
Image Analyst
Image Analyst 2018 年 7 月 12 日
But Dennis already did that "It is way easier to answer your question if you provide the needed information" so I didn't think I need to repeat that, but since you didn't provide it I thought I'd give a link that is more helpful than Dennis's request. It's a link that we often give that spells out all the various things that one might provide, lest one provide only part of the things that would help.
Anyway, since you didn't read the link or follow its helpful suggestions, we still don't have all your code and can't even get to the error you mention because of the missing variables, so we're left to guess at possible reasons instead of knowing what the problem is.
Still willing to help, if you let us.

サインインしてコメントする。

Dennis
Dennis 2018 年 7 月 13 日

0 投票

If rho2() returns a vector your calculation will result in a vector aswell.
q1(j1,:) = (rho2(j1) - rho2(j1-1))*v2(j1-1)./((rho1_final - rho2(j1))*dt);
Please consider initializing q1 so its size doesn't change in every loop iteration.

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

質問済み:

2018 年 7 月 12 日

回答済み:

2018 年 7 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by