Plot a variable within a function
Show older comments
How should I plot Ca vs W curve after function has been evaluated
A0 = [0 1];
Wspan = [0 100];
[W, A] = ode45(@ODEfun, Wspan, A0);
function dYfuncvecdW = ODEfun(W, Yfuncvec)
X = Yfuncvec(1);
y = Yfuncvec(2);
k = 6;
Cao = 0.2;
yao = 1/3;
Fao = 2;
Pao = 10;
epsilon = yao*(1-2-1);
ThetaB = 2;
alpha = 0.02;
Ca = Cao*(1-X)/(1+(epsilon*X))*y;
Cb = Cao*(ThetaB-(2*X))/(1+(epsilon*X))*y;
ra = -k*Ca*Cb^2;
dXdW = -(ra/Fao);
dydW = -alpha*(1+(epsilon*X))/2/y;
dYfuncvecdW = [dXdW;dydW];
plot(W, Ca); %How do I plot this after the ode has been solved?
end
Answers (1)
"How should I plot Ca vs W curve after function has been evaluated"
Return the required variables as the 2nd, 3rd, etc. function outputs, e.g.:
function [dYfuncvecdW,Ca] = ODEfun(W, Yfuncvec)
% ^^
and then solve the ODE like normal:
A0 = [0,1];
Wspan = [0,100];
[W, A] = ode45(@ODEfun, Wspan, A0);
and then afterwards call the function to generate the Ca values. This could be done in a loop or cellfun:
[~,Ca] = cellfun(@ODEfun,num2cell(W),num2cell(A,2),'uni',0);
Ca = cell2mat(Ca);
plot(W,Ca)

Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!