# Plot a variable within a function

12 views (last 30 days)
Elliot Alderson on 21 Oct 2020
Edited: Stephen Cobeldick on 21 Oct 2020
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

Stephen Cobeldick on 21 Oct 2020
Edited: Stephen Cobeldick on 21 Oct 2020
"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) 