collect like terms, and substitute values for i

12 views (last 30 days)
I have this equation:
dT = (a/r)*((T_i+1 - T_i-1)/2*dr) + r*((T_i+1 - T_i + T_i-1)/dr^2) + q/cp*rho;
I want to simplify this and collect like terms, as in T_i+1, T_i and T_i-1.
I also want to be able to plug in values for i, such as 1, n-2, n-1 and so on.
I tried a few things like this:
% Clear the workspace and the command window
clear; clc;
% Declare the symbolic variables
syms a dr T1 Ti T2 r q cp rho
% Define the expression
dT = (a/r)*((T1 - T2)/2*dr) + r*((T1 - Ti + T2)/dr^2) + q/cp*rho;
% Expand the expression to combine like terms
dT_expanded = expand(dT);
% Collect the terms for T1, Ti, and T2
[T1_coeff, T1_term] = coeffs(dT_expanded, T1, 'All');
[Ti_coeff, Ti_term] = coeffs(dT_expanded, Ti, 'All');
[T2_coeff, T2_term] = coeffs(dT_expanded, T2, 'All');
% Display the collected terms for T1, Ti, and T2
disp("Terms for T1:")
Terms for T1:
disp(T1_term)
disp("Terms for Ti:")
Terms for Ti:
disp(Ti_term)
disp("Terms for T2:")
Terms for T2:
disp(T2_term)
;;;;;;
%and more simply
dT = simplify((a/r)*((T1 - T2)/2*dr) + r*((T1 - Ti + T2)/dr^2) + q/cp*rho)
dT = 
% Display the simplified expression
disp(dT);
a1 = collect(dT, T1)
a1 = 
b1= collect(dT, Ti)
b1 = 
c1=collect(dT, T2)
c1 = 
d1=q/(cp*rho)
d1 = 
But neither of these are giving me what I want.

Accepted Answer

Walter Roberson
Walter Roberson on 29 Apr 2023
You do not say clearly what you do want.
% Clear the workspace and the command window
clear; clc;
% Declare the symbolic variables
syms a dr T1 Ti T2 r q cp rho
% Define the expression
dT = (a/r)*((T1 - T2)/2*dr) + r*((T1 - Ti + T2)/dr^2) + q/cp*rho;
% Expand the expression to combine like terms
dT_expanded = expand(dT);
% Collect the terms for T1, Ti, and T2
[T1_coeff, T1_term] = coeffs(dT_expanded, T1, 'All');
[Ti_coeff, Ti_term] = coeffs(dT_expanded, Ti, 'All');
[T2_coeff, T2_term] = coeffs(dT_expanded, T2, 'All');
% Display the collected terms for T1, Ti, and T2
disp("Terms for T1:")
Terms for T1:
disp((T1_term == T1_coeff).')
disp("Terms for Ti:")
Terms for Ti:
disp((Ti_term == Ti_coeff).')
disp("Terms for T2:")
Terms for T2:
disp((T2_term == T2_coeff).')

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!