You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
System of Equations with For loop and symbols
1 view (last 30 days)
Show older comments
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 Comments
darova
on 23 Apr 2020
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
on 23 Apr 2020
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.
Ryan Mulligan
on 23 Apr 2020
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
on 23 Apr 2020
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.
Ryan Mulligan
on 23 Apr 2020
I'm gettting an index error when I try to run this code, "Index exceed the number of array elements (1)"
Ryan Mulligan
on 23 Apr 2020
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
on 23 Apr 2020
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
on 24 Apr 2020
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.
Ryan Mulligan
on 24 Apr 2020
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.
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)



