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

์กฐํšŒ ์ˆ˜: 3 (์ตœ๊ทผ 30์ผ)
Naveen Krish
Naveen Krish 2022๋…„ 3์›” 18์ผ
๋Œ“๊ธ€: Walter Roberson 2022๋…„ 3์›” 21์ผ
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);

๋‹ต๋ณ€ (2๊ฐœ)

Steven Lord
Steven Lord 2022๋…„ 3์›” 18์ผ
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 2022๋…„ 3์›” 18์ผ
ํŽธ์ง‘: Torsten 2022๋…„ 3์›” 18์ผ
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
Naveen Krish
Naveen Krish 2022๋…„ 3์›” 18์ผ
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 2022๋…„ 3์›” 21์ผ
You have to assign to solinit before you use it.
solinit = bvpinit(xmesh,guess);
r = bvp4c(@ode,@bc,solinit);

๋Œ“๊ธ€์„ ๋‹ฌ๋ ค๋ฉด ๋กœ๊ทธ์ธํ•˜์‹ญ์‹œ์˜ค.

์นดํ…Œ๊ณ ๋ฆฌ

Help Center ๋ฐ File Exchange์—์„œ Boundary Value Problems์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ธฐ

์ œํ’ˆ


๋ฆด๋ฆฌ์Šค

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by