How do I generate a matrix based on a formula?

3 ビュー (過去 30 日間)
Jonathon
Jonathon 2011 年 4 月 25 日
コメント済み: Sam Harris 2019 年 10 月 5 日
I am currently trying to solve a system of equations and having a problem doing it with matlab. The most noteworthy problem is I don't want to enter the entire matrix manually as it would be massive. Could anyone tell me how to generate a matrix based on the formula?
The formula itself is as follows: -4T(i,j)+T(i+1,j)+T(i,j+1)+T(i-1,j)+T(i,j-1)= Known value
Effectively i and j are the points on a plain and the entire plain is 9x9.
A second question I have is how would I tell matlab that if i or j is 0 the value at that point is 0 as well as if i or j is 9 the value at that point is 0?
  1 件のコメント
Sam Harris
Sam Harris 2019 年 10 月 5 日
I have a similar problem! Did you figure it out in the end?

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

採用された回答

Sarah Wait Zaranek
Sarah Wait Zaranek 2011 年 4 月 25 日
You can use backslash to solve this:
n = 9;
% Fill in the correct terms for your non-zeros values.
A =diag(-4*ones(n,1),0) + diag(ones(n-1,1),-1) + ...
diag(ones(n-1,1),1) + diag(ones(n-(sqrt(n)+1),1),sqrt(n)+1) +...
diag(ones(n-(sqrt(n)+1),1),-sqrt(n)-1) ;
b would be your known values in a 9x1 matrix. (List them by column all rows in a column and then move to the next column)
Solve by:
x=A\b
You need to worry about boundaries values when + or - j (or i) aren't defined. I ignored them here.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFixed-Point Designer についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by