# Solving PDE in Matlab

Hi all,

I'm a newbie at MATLAB and currently trying to model a chromatographic process, I have a PDE to be solved in the form of c*D(C_RH)/Dz = D(f)/Dz + s (see code below for what functions c, f and s are made of)
I have defined constant values for each of the individual parameters which make up functions c, f and s with the exception of the parameter C_RH which has yet to be solved.
Also z= linspace(0, 1, 5)
t = linspace(0, 1, 20)

Now I have the following matlab code for solving the PDE:

Matlab:
[c,f,s] = pdefun(z,t,C_RH,dC_RHdz)
[c,f,s] = pdex1pde(z,t,C_RH,dC_RHdz)
c = epsilon_t*C_H_cation+epsilon_e*K_a_sucrose;
f = (D_L_sucrose)*epsilon_e*((C_H_cation)+(K_a_sucrose))*d(C_RH)dz;
s = -u*((C_H_cation)+(K_a_sucrose))*d(C_RH)dz;
Note: d(C_RH)dz in matlab code is supposed to represent D(C_RH)/Dz
this is the way PDE terms were written in a matlab example script for solving PDEs.

I am getting a errors saying that the value assigned to c, f and s appears to be unused. Also errors saying 'Parse error at dz: usage might be invalid MATLAB syntax'.

Regards

Last edited by a moderator:

jedishrfu
Mentor
I adjusted your post using code tags to make the matlab code stand out.

With respect to your errors, I think the unused c,f and s error is due to the [c,f,s] lines where they are given values but then in subsequent lines you don't use those values.

It's like saying:

y=x
y=x*x

The first assignment to y was wiped out by the second assignment to y and would result in the warning you got whereas:

y=x
y=x*y

Now the first y is used to compute the second y value and it wasn't thrown away.

For the dz did you mean to multiply it to the preceding factor? If so then you need to use * to indicate it.

Basically I have defined c, f and s first in terms of different parameters (this is essential) and now I need for matlab to solve the following PDE based on the the parameters that I have defined

c(z,t,C_RH,∂C_RH∂z)∂C_RH∂t=z−m∂∂z(zmf(z,t,C_RH,∂C_RH∂z))+s(z,t,C_RH,∂C_RH∂z).

This is my code as it is to attempt to solve the above PDE:

Matlab:
m = 0;
sol = pdepe(m,@pdex1pde,@pdex1ic,@pdex1bc,z,t);
C_RH = sol(:,:,1);
c = epsilon_t*C_H_cation+epsilon_e*K_a_sucrose;
f = (D_L_sucrose)*epsilon_e*((C_H_cation)+(K_a_sucrose))*DC_RHDz;
s = -u*((C_H_cation)+(K_a_sucrose))*DC_RHDz;
[c,f,s] = pdex1pde(z,t,C_RH,DC_RHDz);
The parse error has disappeared but i am still getting the errors saying that c, f and s are unused.
What am I doing wrong here?

kreil
Gold Member
The parse error has disappeared but i am still getting the errors saying that c, f and s are unused.
What am I doing wrong here?
You are still overwriting the values for c, f, and s. When you specify arguments to the left of the = sign, they are output arguments.

Code:
[c,f,s] = pdex1pde(z,t,C_RH,DC_RHDz);
This code is calling pdex1pde with the 4 input arguments and returning the 3 output arguments c,f,s. Since you previously defined c, f, and s as the outputs to the previous lines, this is overwriting those values. The old values are done. After this line executes it is as if these three previous lines didn't exist:

Code:
c = epsilon_t*C_H_cation+epsilon_e*K_a_sucrose;
f = (D_L_sucrose)*epsilon_e*((C_H_cation)+(K_a_sucrose))*DC_RHDz;
s = -u*((C_H_cation)+(K_a_sucrose))*DC_RHDz;
So this is why you still get the "c, f, s appear to be unused". You assign values to them, then overwrite those values. But you don't ever use the values of c, f , and s in any calculation.

I believe the reason you're making this mistake is because on the doc page the code is actually written like this:

Code:
function [c,f,s] = pdex1pde(x,t,u,DuDx)
c = pi^2;
f = DuDx;
s = 0;
This code is different: it defines the function pdex1pde that returns the values c, f, and s. So in this case the assignments to c, f, and s, determine how they are returned.

So altogether, I believe what you want to do is save your function as its own file (I changed the name since pdex1pde is an existing function shipped with matlab):

Code:
function [c,f,s] = myPDE(z,t,C_RH,DC_RHDz)
% Make sure you define the other variables needed for the expressions below
c = epsilon_t*C_H_cation+epsilon_e*K_a_sucrose;
f = (D_L_sucrose)*epsilon_e*((C_H_cation)+(K_a_sucrose))*DC_RHDz;
s = -u*((C_H_cation)+(K_a_sucrose))*DC_RHDz;
Then in your main script, you execute the call to pdepe that calls the above function:
Code:
m = 0;
sol = pdepe(m,@myPDE,@pdex1ic,@pdex1bc,z,t);
C_RH = sol(:,:,1);
I'm not sure why C_RH is defined based on the output of pdepe, yet it is an input to myPDE that pdepe calls to solve?

I highly recommend you read through the examples and parameter descriptions on this page to get an idea for the workflow:
http://www.mathworks.com/help/matlab/math/partial-differential-equations.html

Hope this helps.

Last edited by a moderator:
Hi,

Sorry for the late reply, I had to shift my focus to another project up until now.
I tried your solution and in my new myPDE.m file I am getting an error message saying that there are not enough input arguments.
I suspected this might be because 'DC_RHDz' is undefined, unlike the other variables that myPDE is a function of (not including C_RH):
Code:
function [c,f,s] = myPDE(z,t,C_RH,DC_RHDz)
I tested this theory by inserting the following defintion for 'DC_RHDz'
Code:
DC_RHDz = linspace(0,3600,120);
and the result - myPDE.m file runs successfully with no errors !!

But the problem is that DC_RHDz is not another variable that I can define, it is the partial differential of the unknown variable C_RH with respect to z. So how can I get this PDE to solve without being forced to define a value(s) for DC_RHDz ??

Also for the case where myPDE.m does run successfully, it still causes problems in the PDE Solver file (Design_Model.m) which contains the code:
Code:
m = 0;
sol = pdepe(m,@myPDE,@pdex1ic,@pdex1bc,z,t);
the errors shown in the solver file are as follows:
Code:
Design_Model
Subscripted assignment dimension mismatch.

Error in pdepe/pdeodes (line 359)
up(:,ii) = ((xim(ii) * fR - xim(ii-1) * fL) + ...

Error in odearguments (line 87)
f0 = feval(ode,t0,y0,args{:});   % ODE15I sets args{1} to yp0.

Error in ode15s (line 148)
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...

Error in pdepe (line 289)
[t,y] = ode15s(@pdeodes,t,y0(:),opts);

Error in Design_Model (line 45)
sol = pdepe(m,@myPDE,@pdex1ic,@pdex1bc,z,t);
>>
I have no idea what any of these errors mean and it is really bizarre because I only have 45 lines of code, I don't see how I can have errors on lines after line 45.

PS: The link for the web page you provided didn't work

kreil
Gold Member
Sorry the link didn't work, it has been fixed. I recommend you read that page and try to reimplement your code using the processes outlined in the examples. Let me know how that works out, and if your code still doesn't run we can fix it from there (if that's the case post all of the code so I would be able to run it)

The subscripted assignment dimension mismatch occurs when you try to assign a variable to something it doesn't fit in. For example, if you try to assign a vector to a single element in a matrix.

Thanks, I'll give it a try and let you know.