File: bvpfcn.m Line: 1 Column: 23 Invalid use of operator.

3 views (last 30 days)
Hi, I'm doing a project on chlorine decay in water distribution pipes, to solve this ODE with the following Matlab's BVP4C function . I have wrriten the code but I need to plot the profile C vs x. I have tried searching in MATLAB Answers pertaining to my topics of study, but to no avail. The MATLAB code and the error message are shown below.
At steady state, this can be described by: 𝐷*𝑑^2𝐶/𝑑𝑥^2 − 𝑈*𝑑𝐶/𝑑𝑥 − 𝐾𝐶 = 0 where, 𝐶 is the chlorine concentration (mg/L) at any location 𝑥 (m) along the pipe, 𝐷 is the diffusion coefficient (m2 /s), 𝑈 is the flow velocity in the pipe (m/s), and 𝐾 is the first order decay coefficient (s-1 ). To solve the aforementioned second order ODE, two boundary conditions must be known at the inlet and the outlet of the pipe (i.e., 𝐶 = 𝐶𝑖𝑛 @ 𝑥 = 0, and 𝐶 = 𝐶𝑜𝑢𝑡 @ 𝑥 = 𝐿, where L is the pipe length).I have also attached the code/data for your convenience. I appreciate your help with troubleshooting the problem.Thanks for considering my request.
function dydx = bvp4c(@ode,@bc,solinit); % equation to solve
plot(r.x,r.y(1,:),'--o');
title('chlorine decay')
xlabel('pipe length(m)');
ylabel('Concentration (mg/L)');
function dydx = ode(x,y)
D=0.1; % m2/s
U=1; % m/s
K = 1*10^-6; % 1/s
dydx = [y(2);(U*y(2)+k*y(1))/D];
end
function res = bcfcn(ya,yb) %boundary conditions
res = [ya(1)-3 yb(1)-2];
end
function g = guess (x) % initial guess for y and y'
guess = [2;1];
xmesh = linspace(0,1,5);
solinit = bvpinit(xmesh,guess);

Answers (2)

Steven Lord
Steven Lord on 18 Mar 2022
When you define a function you need to specify the input arguments as the names of the variables in which the user's input will be stored in the function. You cannot specify them as expressions.
When you call a function you need to pass in an expression. Specifying an unknown variable name as an input argument will not work.
You also don't want to call your function bvp4c since there's already a function by that name in MATLAB.
y = mytimestwo(1:10) % Calling the function I can pass in an expression
y = 1×10
2 4 6 8 10 12 14 16 18 20
y = mytimestwo(z) % Calling the function I cannot pass in an unknown name
Unrecognized function or variable 'z'.
% Defining the function I specify a variable name, in this case theInput
%
% When I called this function as y = mytimestwo(1:10) above MATLAB will
% assign 1:10 to the variable theInput inside that mytimestwo call.
function theOutput = mytimestwo(theInput)
theOutput = 2*theInput;
end
% Were I to define a function like below it would error for the same
% reason your function definition would error
%{
function theOutput = mytimestwo(1:10) % Expressions not allowed here
theOutput = 2*theInput;
end
%}

Torsten
Torsten on 18 Mar 2022
Edited: Torsten on 18 Mar 2022
In principle, this is a direct copy of the first example for bvp4c in the MATLAB documentation.
You only had to give different values to the variables.
function main
xmesh = linspace(0,1,10);
solinit = bvpinit(xmesh, @guess);
sol = bvp4c(@bvpfcn, @bcfcn, solinit);
plot(sol.x, sol.y, '-o')
end
function dydx = bvpfcn(x,y)
D = 0.1; % m2/s
U = 1; % m/s
K = 1*10^-6; % 1/s
dydx = [y(2);(U*y(2)+K*y(1))/D];
end
function res = bcfcn(ya,yb)
res = [ya(1)-3
yb(1)-2];
end
function g = guess(x)
g = [3-x;-1];
end
  24 Comments
Naveen Krish
Naveen Krish on 18 Mar 2022
Can you please rectify where i did wrong? and kindly plot C vs x. I already spend too much time on this problem
Walter Roberson
Walter Roberson on 21 Mar 2022
You have to assign to solinit before you use it.
solinit = bvpinit(xmesh,guess);
r = bvp4c(@ode,@bc,solinit);

Sign in to comment.

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!