Clear Filters
Clear Filters

Why does matlab request me to define a for-loop indexing term?

1 view (last 30 days)
Hi everyone,
I am trying to run the attached code, but matlab gives this error:
Undefined function or variable 'k'.
Error in SlurryCaseODE45Feb18>DifEq (line 212)
dcdt(1) = (1/V_Headspace) * (F * CSO2_in - F * c(1) )- ((((c(1)*R*T - ((c(3)*CH(k)^2)/(CH(k)^2 + KSO2*CH(k) + KSO2*KHSO3)) *
HSO2))/((1/kga) + (HSO2/(1 + ((DCa2 .* c(5))/(DSO2 .* c(3)))) * kLa_SO2))));
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in SlurryCaseODE45Feb18 (line 142)
[t,Cv]=ode45(@DifEq,tspan,c0,Options);
Why does matlab request me to define k?
Please help.

Accepted Answer

Walter Roberson
Walter Roberson on 14 Feb 2019
Variables defined inside one workspace are not visible inside another workspace (the function) unless they are specifically passed in or otherwise imported.
However, you have
for k = 1: length(t)
[t,Cv]=ode45(@DifEq,tspan,c0,Options);
end
Even if you were to pass k in specifically for use, then you would have the problem that you are overwriting t and Cv each iteration of k, so you would be throwing away all of the results except the last one.
You also use tv in several plot statements but you have not assigned anything to tv anywhere.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!