How can I use one matrix to assign values to a second matrix using a for loop?

1 回表示 (過去 30 日間)
Harold Sandahl
Harold Sandahl 2019 年 11 月 29 日
コメント済み: Harold Sandahl 2019 年 11 月 29 日
I need to create a diffusion simulation for class and I am stuck on the linear algebra. The first point needs to remain constant and the second point begins to be changed. I need the next point to be calculated through the equation Ci + phi*(Ci-1 - 2*Ci + Ci+1). It appears that I am doing something wrong with the loop itself. Any help is much appreciated. Thank you.
matrix = [1 1 1 1 1 1 1 1 1 1];
vector = [0 0 0 0 0 0 0 0 0 0];
phi = 0.5;
n=1;
for n=1:1:10
if n = 1
vector(n)=matrix(1)
else
vector(n)=matrix(n) + phi*(matrix(n-1)+matrix(n+1)-2*matrix(n));
end
end
  2 件のコメント
KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 11 月 29 日
編集済み: KALYAN ACHARJYA 2019 年 11 月 29 日
No issue with Ci-1 (previous data), but how you get the next value of Ci+1?
Harold Sandahl
Harold Sandahl 2019 年 11 月 29 日
C is a n by 1 matrix and the i represented the row of the matrix. So the i-1 means the previous row, the i means the current row, and the i+1 means the next row.

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

採用された回答

ME
ME 2019 年 11 月 29 日
Based on your definition there is nothing to diffuse. If the values is the same all along your domain then there won't be any change because you will always have vector(n) = matrix(n) as the rest of your expression will cancel out.
I would also have thought you'd get an error of index exceeds the number of array elements. This would be because you are trying to take Ci+1 as the point just outside of your arrays. To fix that issue you'll have to consider and implement some kind of boundary condition on the n=10 end.
I hope that helps! If you still have problems then you may need to explain exactly what issues you are having.
  1 件のコメント
Harold Sandahl
Harold Sandahl 2019 年 11 月 29 日
This was just an attempt to get the code to run before I implement actual values into it. The boundary conditions make sense, but as it stands I am getting Incorrect use of '=' operator. To assign a value to avariable, use '='. To compare values for equality, use '=='. for every equal sign in the for loop.
I have not gotten an index error, but I will define both boundaries and see if that fixes things.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by