- https://www.mathworks.com/help/symbolic/sym.matlabfunction.html
- https://www.mathworks.com/help/matlab/ref/ode45.html?searchHighlight=ode45&s_tid=srchtitle_support_results_1_ode45
How to set the number of independent variables of differential equations automatically?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
clc,clear;
R = 8.314;
g = 9.8;
N = input('species number:');
for i = 1:N
name{i} = input('species:','s');
Mi(i) = input('moler weight of species:');
m(i) = input('entrance mass fraction of species:');
rho0(i) = input('initial density of species:');
mirho0(i) = m(i)/rho0(i);
end
syms T z x
%%
%initial moler concentration
sum_mirho0_1 = 0;
for i = 1:N
sum_mirho0 = sum_mirho0_1+mirho0(i);
sum_mirho0_1 = sum_mirho0
end
for i = 1:N
rho0av = 1/sum_mirho0
c0(i) = rho0av*m(i)*1000/Mi(i);
end
%%
dTdz = input('temperature gradient(K/m):')
T0 = input('entrance temperature(K):')
T = T0 + dTdz*1000*z;
%thermal diffusion factor
%%
syms z
c = sym('c',[N,1])
sum_c = sum(c);
sum_c01 = 0;
for i = 1:N
sum_c0 = sum_c01+c0(i)
sum_c01 = sum_c0
end
%%
for i = 1:N
alpha(i) = -(log((c(i)/c0(i))*((sum_c0-c0(i))/(sum_c-c(i)))))*(log(T/T0));
%individual height
Hi(i) = 1/((Mi(i)*g)/(R*T));
%ode for every species
dc(i) = c(i)*(-(27*((1 + alpha(i))/T)-(1/Hi(i))))
end
%%
f = matlabFunction(dc');
F = @(z,c)f(c(1),c(2),z)
%%
[z,c] = ode45(F,[0:0.1:100],[c0(1),c0(2)])
In this code,number of species equals to the number of functions.,for example,if species number is 2,so when I set ode,I should type:
F = @(z,c)f(c(1),c(2),z)
I want to omit this step,how can I modify the code to automatically identify the number of independent variables(c(1),c(2) and so on) and set it in the form that ode equation can identify(it is the form'c(1),c(2)...' rather than 'c1,c2' or others).
Thanks a lot for your help.
0 commentaires
Réponses (1)
Kartik Saxena
le 5 Jan 2024
Hi,
I understand that you want to identify the number of independent variables automatically.
To automate the process of creating a function handle that can be used with `ode45`, you can leverage the fact that the `f` function returned by `matlabFunction` can accept a vector of inputs. This way, you don't need to manually specify each component of `c` when defining the function handle `F`. Instead, you can pass the entire vector `c` to `f`.
Here's a code snippet of how that would look like:
f = matlabFunction(dc', 'Vars', {c, z});
F = @(z, c) f(c, z);
[z, c] = ode45(F, [0:0.1:100], c0(1:N));
Refer to the following MathWorks documentations to for information about the use of 'matlabFunction' and 'ode45':
I hope this resolves your issue.
0 commentaires
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!