Solve PDE in MATLAB: Errors & Tips

  • MATLAB
  • Thread starter MindOverMatter
  • Start date
  • Tags
    Matlab Pde
In summary, the code is trying to solve a PDE but is getting errors saying that c, f, and s are unused. The solution is to save the function as its own file and then call pdepe with the function definitions.
  • #1
MindOverMatter
4
0
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'.Can anybody me help please?

Regards
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
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.
 
  • #3
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?
 
  • #4
MindOverMatter said:
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:
  • #5
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
 
  • #6
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.
 
  • #7
Thanks, I'll give it a try and let you know.
 

1. What is a PDE?

A PDE, or partial differential equation, is a mathematical equation that involves multiple variables and their partial derivatives. It is used to model physical phenomena in fields such as physics, engineering, and economics.

2. Why is MATLAB commonly used to solve PDEs?

MATLAB is a powerful software tool that is specifically designed for numerical computing. It has built-in functions and features that make it well-suited for solving complex PDEs, including advanced algorithms and visualization tools.

3. What are some common errors encountered when solving PDEs in MATLAB?

Some common errors include improper input of equations or boundary conditions, incorrect use of functions, and issues with convergence. It is important to carefully check the code and troubleshoot any errors that arise.

4. How can I improve the accuracy of my PDE solution in MATLAB?

One way to improve the accuracy is to use a finer grid or mesh in the numerical solution. Additionally, using higher-order methods and adaptive algorithms can also improve the accuracy of the solution.

5. What are some tips for efficiently solving PDEs in MATLAB?

Some tips include using vectorization to speed up computations, avoiding unnecessary loops, and utilizing built-in functions and algorithms. It is also helpful to carefully plan and organize the code before starting the solving process.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
927
  • MATLAB, Maple, Mathematica, LaTeX
2
Replies
41
Views
8K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • Introductory Physics Homework Help
Replies
4
Views
1K
Back
Top