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

2 次查看(过去 30 天)
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);

回答(1 个)

Walter Roberson
Walter Roberson 2022-12-1
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 个评论
Michael
Michael 2022-12-1
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?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Applications 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by