I need to solve in MATLAB the following system (1):
\begin{align} &\frac{\partial \theta}{\partial t} + u \frac{\partial (\rho\theta)}{\partial x} = \frac{1}{\text{Pe}_T} \frac{\partial^2 \theta}{\partial x^2} + \Phi, \tag{15}\\ &\frac{\partial \eta}{\partial t} = \Phi , \tag{16}\\ & \rho = \theta_0/(\theta + \theta_0) , \tag{17}\\ & \Phi = \beta (1-\eta)\exp\left(-\frac{\mathcal E}{\theta + \theta_0}\right) \tag{18} \end{align}
And for it I would like to use these datas and scheme (Crank-Nicolson)
With the boundary condition $\theta(x_0,t) = 0$ and $\eta(x_0,t) = 1$ at $x_0 = 0$ for $t\geq 0$.
I really do not know how I can do this on MATLAB. I tried to put on MATLAB all my datas, as you can see but I do not know as I can finish.
About this code I have got the following error: Line: 74 Assignment to x not supported. Variable has the same name as a local function.
Follow the code. I've got a hard activity and I don't have experience on MATLAB, indeed my deadline is hard, so I'm sorry for my mistakes.
Many thanks for any help!
tic
clear all
close all
%% INPUT PARAMETERS
xmax=0.05; % x maximal value
xmin=0; % x maximal value
tmax=0.008; % x maximal value
tmin=0; % x maximal value
dt=10^(-5); % time step value
tol=10^(-6); % precison on the staionnary solution
Pet=1406;
beta=7.44*10^(10);
epsilon=93.8;
theta0=3.67;
u=3.76;
phi==beta*(1-eta)*e^(-epsilon/(theta+theta0));
rho==theta0/(theta+theta0);
V0==[theta;eta];
% discretise the domain
dX=(xmax-xmin)/500;
%%
V=V0;
VCN=V
%%
% Initial conditions
for i=0: 800;
if x==0;
theta(x,t(i))==0;
eta(x,t(i))==1;
end
end
for i=0: 800;
if t==0;
theta(x(i),t)==0;
eta(x(i),t)==0;
end
end
% loop through time
nsteps= tmax/dt;
for n=1 : nsteps
% calculate the scheme
for i = 1 : 800
for j=1 : 800
theta(x(j),t(i+1))+u*dt*(1/(4*dX))*(rho(x(j+1),t(i+1))*theta(x(j+1),t(i+1))-rho(x(j-1),t(i+1))*theta(x(j-1),t(i+1)))-(dt/(2*Pet*dX*dX))*(theta(x(j-1),t(i+1))-theta(x(j),t(i+1))+theta(x(j+1),t(i+1)))-(dt/2)*phi(x(j),t(i+1))== theta(x(j),t(i))-u*dt*(1/(4*dX))*(rho(x(j+1),t(i))*theta(x(j+1),t(i))-rho(x(j-1),t(i))*theta(x(j-1),t(i)))+ (dt/(Pet*2*dX*dX))*(theta(x(j-1),t(i))-2*theta(x(j),t(i))+theta(x(j+1),t(i)))+(dt/2)*phi(x(j),t(i));
(dt/2)*phi(x(j),t(i+1))+eta(x(j),t(i+1))== eta(x(j),t(i))-(dt/2)*phi(x(j),t(i));
end
end
% update V
V=VCN;
end
% plot solution
set(fig,'YData',V(1,:));
set(tit,'String',strcat('n =',num2str(n)));
drawnow;
function x=x(j);
x(j)=j*dx
end
function t=t(i);
t(i)=i*dt
end
function eta=eta(x,t);
end
function theta=theta(x,t);
end
function phi=phi(x,t);
phi(x,t)=beta*(1-eta(x,t))*e^(-epsilon/(theta(x,t)+theta0));
end
(1) G. Chapiro, A.E.R. Gutierrez, J. Herskovits, S.R. Mazorche, W.S. Pereira (2016): "Numerical Solution of a Class of Moving BoundaryProblems with a Nonlinear Complementarity Approach", J. Optim. Theory Appl. 168(2), 534-550. doi:10.1007/s10957-015-0816-7

Let us introduce the vector ${\bf U} = (\theta, \eta)^\top$ of unknowns. Since $\partial_t {\bf U}$ is nonlinear in ${\bf U}$, the Crank-Nicolson method requires to invert the nonlinear algebraic system $F_\Delta (\theta,\eta) = 0$ from Eq. (30) at each time step. This step provides the updated values ${\bf U}_m^{n+1}$ from the current values ${\bf U}_m^{n}$. The discrete version of $\partial_t \eta = \Phi$ writes $F^2_\Delta (\theta, \eta) = 0$, where $$ F^2_\Delta (\theta, \eta) = \eta_m^{n+1} - \eta_m^n - \frac{\Delta t}{2} \left(\Phi_m^{n+1} + \Phi_m^n\right) $$ (note the sign mistake in Eq. (32)). We introduce the function $\varphi$ of the variable $\theta$ such that $\Phi = (1-\eta) \varphi$. Thus, $F^2_\Delta (\theta, \eta) = 0$ can be solved analytically, providing $$ \eta_m^{n+1} = \frac{\eta_m^{n} + \frac12 {\Delta t}\, (\Phi_m^n + \varphi_m^{n+1})}{1 + \frac12 {\Delta t}\, \varphi_m^{n+1}} \, . $$ In the general case, only the equation $F^1_\Delta (\theta, \eta) = 0$ remains to solve with respect to the unknown $\theta_m^{n+1}$. Introducing the function $f = \rho\theta$ of the variable $\theta$, one may write \begin{aligned} F^1_\Delta (\theta, \eta) &= \theta_m^{n+1} - \theta_m^n + u\frac{\Delta t}{4\, \Delta x} \left(f_{m+1}^{n+1} - f_{m-1}^{n+1} + f_{m+1}^{n} - f_{m-1}^{n}\right) \\ & - \frac{\Delta t}{2\, \text{Pe}_T \, \Delta x^2} \left(\theta_{m+1}^{n+1} - 2\theta_{m}^{n+1} + \theta_{m-1}^{n+1} + \theta_{m+1}^{n} - 2\theta_{m}^{n} + \theta_{m-1}^{n}\right) \\ & - \frac{\Delta t}{2} \left(\Phi_m^{n+1} + \Phi_m^n\right) \end{aligned} (note the typo in Eq. (31)). The Crank-Nicolson method can be implemented in Matlab with
fsolvefrom the Optimization Toolbox as follows:with the function
fun.mdefined byThe Matlab implementation above is not optimal since it doesn't exploit the expression of $\eta_m^{n+1}$. Nevertheless, it must have less programming weaknesses than OP's version, which includes basic Matlab programming mistakes such as duplicate names, inappropriate assignments, data types, and scheme implementation.