現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
System of Equations with For loop and symbols
2 ビュー (過去 30 日間)
古いコメントを表示
sym T
C = .2;
N = 6;
gamma = .1;
delR = (1-C)/N;
Ri = C+(i-1)*delR;
A = sym(zeros(N+1));
B = sym(zeros(N+1,1));
for i = 2:N
A(i,:) = ((1/delR^2-1/(Ri*2*delR)))*T(i-1)-((2/delR^2)+gamma^2)*T(i)+((1/delR^2)+(1/(Ri*2*delR)))*T(i+1)==0;
[A,B] = equationsToMatrix([A(i,:)],[T(i,:)]);
end
X = linsolve(A,B)
This is currently what I have written, I'm trying to solve a system of equations where using information provided by the user, C,N,gamma, it will use the formula displayed by what A(i,:) is equal to to create a system of equations and will then solve for the values of T_0 to T_N. I was able to solve the system without the for loop but cannot seem to figure out how to do it with the for loop. I'm sure there are many mistakes in this code, I'm seeking any sort of guidance I can get. I'm stuck on an error with the symbols, "Undefined function 'T' for input arguments of type 'double' ". Any sort of help would be apprecitated greatly.
16 件のコメント
darova
2020 年 4 月 23 日
If i understood this correctly θ is uknown variable
To solve any system of equations numer of uknowns shoould be equal to number of equations
But in this case number of uknown is always bigger
eq1: T0 T1 T2
eq2: T1 T2 T3
eq3: T2 T3 T4
5 uknowns, 3 equations. Am i missing something?
Ryan Mulligan
2020 年 4 月 23 日
sorry, let me try to give a better explaination, so the goal is to be able to turn this into a function that takes values including N which is the number of "nodes" so the formula will use i values from 1 to N+1 where 2 to N will use the above formula, it is also given that T1 = 1. TN+1 would use a branching conditional statement based on a choice from three other formulas which I belive I can figure out on my own. So for this example I just put in random values for N and other constants that would be supplied by the user when I make this into a function file. So for i = 1 you plug that into the formula above, then you do the same for i = 2 and so on until you reach i=N. I don't know if what I'm saying here is helping much.
darova
2020 年 4 月 23 日
Is it correct?
You are saying: i have 5 uknown, 3 main equations and 2 addititona equations
Ryan Mulligan
2020 年 4 月 23 日
eq0 = b==1;
eq1 = 23.4375*a-88.1275*b+54.6875*c ==0;
eq2 = 30.381*b - 88.125*c+44.743*d ==0;
eq3 = 30.381*c - 88.125*d+45.072*e==0;
eq4 = 34.37*d-88.125*e+43.65*f==0;
eq5 = 35.34*e-88.125*f+42.78*g==0;
eq6 = 35.93*f-88.125*g+42.187*h==0;
eq7 = e - 4*f + 3*g==0;
this is the system of equations I got with set values of N = 5, C = .2, and gamma = sqrt(10) and T1 = 1. T0 = a, T1=b, T2 = C, and so on. So this for loop has to do with rows 2 to N as the equation above dictates those rows, row 1 is found by the fact that T1 = 1. I want to use this loop for i values 2 to N where they will fill in rows 2 to N respectively, so if N=2 then you will have the given T1, T2, T3 in the given formula above, then move onto N=3 or i=3 to get T2,T3,T4 from fromula above to create the rows 2 through N of a system of equations, row 1 will be added by formula T1=1. Again I'm sorry if I'm butchering this explaination and thank you for your patience.
Ryan Mulligan
2020 年 4 月 23 日
okay so I found some mistakes I belive I made before and wrote a new script, maybe this might help me explain it...
N = 5;
C = .2;
gamma = sqrt(10);
i = [2:N];
delR = (1-C)/N;
Ri = C+(i-1)*delR;
syms a b c d e f
eq0 = a-1==0;
eq1 = (((1/delR^2)-(1/(Ri(:,1)*2*delR))*a))-((2/delR^2+gamma^2)*b)+((1/delR^2+(1/(Ri(:,1)*2*delR))*c))==0;
eq2 = (((1/delR^2)-(1/(Ri(:,2)*2*delR))*b))-((2/delR^2+gamma^2)*c)+((1/delR^2+(1/(Ri(:,2)*2*delR))*d))==0;
eq3 = (((1/delR^2)-(1/(Ri(:,3)*2*delR))*c))-((2/delR^2+gamma^2)*d)+((1/delR^2+(1/(Ri(:,3)*2*delR))*e))==0;
eq4 = (((1/delR^2)-(1/(Ri(:,4)*2*delR))*d))-((2/delR^2+gamma^2)*e)+((1/delR^2+(1/(Ri(:,4)*2*delR))*f))==0;
eq5 = d - 4*e + 3*f ==0;
[A,B] = equationsToMatrix([eq0,eq1,eq2,eq3,eq4,eq5],[a,b,c,d,e,f]);
X = linsolve(A,B)
% a = t1 b = t2 c = t3 d = t4 e = t5 f = t6
% eq0 is given by the fact that t1 or a = 1
% eq5 is given by a choice of three formulas to choose from
% eq1-4 is given from the original equation
%6 unknowns and 6 formulas
%need to make eq1 through eq5 a for loop where eqs 1 through 4 are used for
%i values 2 through N and eq 5 is going to be conditionaly selcted based on
%a value entered by user, either 1 2 or 3
%this should give us values when solved of theta_1 through N
%for this script N, C, and gamma were chosen.
%Ri is also based on values of i
So since i starts at 2 and goes to N the T0 i refered to earlier should not exist and neither should T7. every row between the first and last is given by the first formula I posted. row 1 is given by t1 = 1 and the last row is given by a choice of three formulas based on if the user enters a 1,2 or 3. So eq 5 will be changing from case to case.
darova
2020 年 4 月 23 日
I think it's easy to fill amtrix without symbolic calculations
A1(1,1) = 1;
for i = 2:N
A1(i,i-1) = 1/delR^2 - 1/Ri(i-1)/2/delR;
A1(i, i) = -2/delR^2 - gamma^2;
A1(i,i+1) = 1/delR^2 + 1/Ri(i-1)/2/delR;
end
A1(N+1,end-2:end) = [1 -4 3];
Results

Ryan Mulligan
2020 年 4 月 23 日
I'm gettting an index error when I try to run this code, "Index exceed the number of array elements (1)"
Ryan Mulligan
2020 年 4 月 23 日
I did but I'm still getting error, says its in line....
A1(i,i-1) = 1/delR^2 - 1/Ri(i-1)/2/delR;
says number of array elements is 1
darova
2020 年 4 月 23 日
Is you Ri varibale is array? Are you running this code?
clc,clear
N = 5;
C = .2;
gamma = sqrt(10);
i = [2:N];
delR = (1-C)/N;
Ri = C+(i-1)*delR;
A1 = zeros(N+1);
A1(1,1) = 1;
for i = 2:N
A1(i,i-1) = 1/delR^2 - 1/Ri(i-1)/2/delR;
A1(i, i) = -2/delR^2 - gamma^2;
A1(i,i+1) = 1/delR^2 + 1/Ri(i-1)/2/delR;
end
A1(N+1,end-2:end) = [1 -4 3];
Ryan Mulligan
2020 年 4 月 24 日
This code works for me, thank you! But now how would you have this code solve for the T values, that's why i was using syms before.
darova
2020 年 4 月 24 日
YOu mean B vector? Here it is

B1 = zeros(N+1,1);
B1(1) = 1;
And then solve for T as you did previously
T1 = A1\B1;
Ryan Mulligan
2020 年 4 月 24 日
Yes thank you so much, I was forgetting to add the first row of B as 1, that's where my error was. Thank you again.
回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)


