Hi, I have written a code using the pdepe solver, but i get nothing when i run. Only my variables show in the workspace. Any help please

1 view (last 30 days)
Below is the code:
clear; clc
%% Calculation of the amount of Iron released into the soluble aqueous phase
% This is a PDE varying in spatial distance x and time t
%% Defining my constants
KD = 0.05;
Csat = 0.03;
L = 2; % Length of tube in metres
v = 4360; % Coolant flow rate in m/s
n = 40; % Number of nodes
dx = L/n;
function pdex1
m = 1;
x = 0:dx:L
t = linspace(0,3600.20);
sol = pdepe(m,@pdefun,@pdeic,@pdebc,x,t)
u = sol(:,:,1); % Extracting the first solution component as u
surf(x,t,u)
title('Numerical solution over 20 mesh points')
xlabel('Distance, x')
ylabel('Time, t')
v = 4360; % m/s
% Define a function for Csat that depends on T, pH, etc
end
%% Defining the PDE function
function [c,f,s] = pdefun(x,t,u,DuDx)
c = 1;
f = [-vu]*DuDx;
s = KD*u - KD*Csat;
end
%% Defining the initial conditions
function u0 = pdeic(x)
u0 = 0;
end
%% Defining the boundary conditions
function [pL,qL,pr,qr] = pdebc(xL,uL,xr,ur,t)
pL = 0;
qL = 1;
pr = 0;
qr = 1;
end

Accepted Answer

Walter Roberson
Walter Roberson on 26 Oct 2020
Edited: Walter Roberson on 26 Oct 2020
Correct.
Your code clears variables and clears the screen.
Then it defines 6 variables.
Then it defines several functions.
At no point does it call the functions, so there is no work for it to do in the script after it defines the variables.
Note that your functions use vu without defining vu
Note that your functions do not have access to any of the variables defined in the script portion, such as L. Your functions are not nested functions with shared variables. In order to have shared variables, there is a strict syntax format that is needed:
function outer_function %possibly with inputs and outputs
shared variables assigned to
code calling functions
function shared_function %possibly with inputs and outputs
code referring to shared variable
end
end
Notice that there must be a function with a matching end that is after the complete definition of the functions that use the shared variables. Notice that all shared variables must be assigned to before the shared functions are called, even if the shared variables will be written to by the functions . Notice that the nested functions must be complete with a function statement through to a matching end statement.
Your code instead tries to define some variables inside a script, and then tries to use the variables inside the functions. Variables first assigned to inside a script are never shared (but scripts that are called within a function can make use of shared variables that are already defined inside the function... but in such a case, the script would not be able to assign to any variable that is not already defined inside the function.)
  4 Comments
Walter Roberson
Walter Roberson on 26 Oct 2020
Delete your line
clear; clc
Move your line
function pdex1
to the first line of the file.
Move the line
end
that is immediately after
% Define a function for Csat that depends on T, pH, etc
to the last line of the file. To emphasize, there must not be an end at its current place after that comment, and the file must end in two end statements in a row.
Note: the name of the function you define on the first line, pdex1 ideally should match the name of the file you have stored all of this code in.

Sign in to comment.

More Answers (1)

Nana Yaw Angu
Nana Yaw Angu on 26 Oct 2020
Thanks a lot
Got it now
This is the code now:
%% Calculation of the amount of Iron released into the soluble aqueous phase
% This is a PDE varying in spatial distance x and time t
function pdex1pde
m = 1;
L = 2; % Length of tube in metres
v = 4360; % Coolant flow rate in m/s
n = 40; % Number of nodes
dx = L/n;
x = 0:dx:L;
t = linspace(0,3600,20);
sol = pdepe(m,@pdefun,@pdeic,@pdebc,x,t) ...... This is line 12
u = sol(:,:,1); % Extracting the first solution component as u
surf(x,t,u)
title('Numerical solution over 20 mesh points')
xlabel('Distance, x')
ylabel('Time, t')
v = 4360; % m/s
KD = 0.05;
Csat = 0.03;
%% Defining the PDE function
function [c,f,s] = pdefun(x,t,u,DuDx)
c = 1;
f = [-vu]*DuDx;
s = KD*u - KD*Csat;
end
%% Defining the initial conditions
function u0 = pdeic(x)
u0 = 0;
end
%% Defining the boundary conditions
function [pL,qL,pr,qr] = pdebc(xL,uL,xr,ur,t)
pL = 0;
qL = 1;
pr = 0;
qr = 1;
end
end
After running i got the error messages below:
Error: File: pdepe.m Line: 2 Column: 24
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax
error. To construct matrices, use brackets instead of parentheses.
Error in pdex1pde (line 12)
sol = pdepe(m,@pdefun,@pdeic,@pdebc,x,t)
  7 Comments
Walter Roberson
Walter Roberson on 26 Oct 2020
You are calling upon a pdepe that is not the Mathworks pdepe.
What output do you see if you give the command line command
which -all pdepe
Note: if you do not execute that command and copy the output for us to see, then I am going to give up assisting you. I already asked twice before this.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!