Plot a variable within a function
조회 수: 5 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (1개)
Stephen23
2020년 10월 21일
편집: Stephen23
2020년 10월 21일
"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)
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!