Need help getting a code to solve for a variable changing in size with a matching number of equations

2 views (last 30 days)
CODE:
clear;
clc;
%Given;
L = 1;
k = 28;
h = 45;
M = 5;
Eqn = zeros(1,M);
T = zeros(1,M);
q_gen = 1*(10^5);
T(1) = -196;
T_infite = 30;
%Loops
m = 2;
while m <= M
X = 0:L/(M-1):L;
if m < M
Eqn(m) = ((T(m-1) -2*T(m) + T(m+1))/(X(m)^2)) * (q_gen/k) == 0;
T(m) = solve(Eqn(m), T);
elseif m == M
Eqn(m) = h*(T_infite - T(1)) + k* ((T(m) - T(m+1))/X) * (q_gen*X/2) == 0;
T(m) = solve(Eqn(m), T);
end
m = m+1;
end
plot(T);
ERROR:
Check for incorrect argument data type or missing argument in call to function 'solve'.
Error in Project (line 21)
T(m) = solve(Eqn(m), T);

Answers (1)

Walter Roberson
Walter Roberson on 1 Dec 2022
q_gen = 1*(10^5);
Numeric
T(1) = -196;
That is a specific numeric value.
X = 0:L/(M-1):L;
That is a vector of numeric values. (Note: consider using linspace instead. And move it to before the loop since it does not change.)
Eqn(m) = ((T(m-1) -2*T(m) + T(m+1))/(X(m)^2)) * (q_gen/k) == 0
Everything on the left hand side is numeric, with no symbolic variables at all. This will be a calculated numeric value "==" 0, which is going to immediately evaluate to true or false, not to an equation.
T(m) = solve(Eqn(m), T);
Eqn(m) is numeric and T is numeric. There is no solve() function that accepts two numeric values.
  2 Comments
Michael
Michael on 1 Dec 2022
clear;
clc;
%Given;
L = 1;
k = 28;
h = 45;
M = 5;
Eqn = zeros(1,M);
q_gen = 1*(10^5);
T_n = -196;
T_infite = 30;
X = 0:L/(M-1):L;
%Loops
m = 2;
while m <= M
syms T
if m == 2;
Eqn(m) = ((T_n -2*T(m) + T(m+1))/(X(m)^2)) * (q_gen/k) == 0;
T_n = solve(Eqn(m), T);
elseif m > 2 && m < M
T_n = ((T_n -2*T(m) + T(m+1))/(X(m)^2)) * (q_gen/k) == 0;
elseif m == M
Eqn(m) = h*(T_infite - T_n) + k* ((T(m) - T(m+1))/X) * (q_gen*X/2) == 0;
T_n = solve(Eqn(m), T);
end
T_m(m) = T_n;
m = m+1;
end
plot(T_m,X);
Could this code potentially work instead if I fix the indexing error?

Sign in to comment.

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!