Plot can no longer recognize my variable

2 views (last 30 days)
I have the following string of code. It was working totally fine, but now cannot recognize one of my variables? I have not change anything relevent to that portion of the code:
yData=[3.0000 0.3548; 5.0000 0.4322; 7.0000 0.4871; 9.0000 0.5171;...
11.0000 0.5315; 13.0000 0.5346; 15.0000 0.5351; 17.0000 0.5272;...
19.0000 0.5251; 21.0000 0.5248; 23.0000 0.5189; 25.0000 0.5103;...
27.0000 0.4958; 29.0000 0.4866; 31.0000 0.4885; 33.0000 0.4680;...
35.0000 0.4599; 37.0000 0.4638; 39.0000 0.4502; 41.0000 0.4368;...
43.0000 0.4325; 45.0000 0.4107; 47.0000 0.4191; 49.0000 0.4110;...
51.0000 0.4033; 53.0000 0.3908; 55.0000 0.3907; 57.0000 0.3749;...
59.0000 0.3717; 61.0000 0.3737; 63.0000 0.3648];
% solve the optimization problem here
options = optimoptions('fmincon','Algorithm','sqp'); % use SQP algorithm
A = []; % linear inequality constraints - NONE
b = []; % NONE
Aeq = []; % linear equality constraints - NONE
beq = []; % NONE
lb = [0.36, 0.05]; % lower bounds on x - given in problem statement
ub = [0.44, 0.06]; % upper bounds on x - given in problem statement
f = @(p)obj(p,yData);% objective function
nonlcon = [];
p0 = [0.4, 0.055];% initial guess from previous problems
[p,fval,exitflag,output,lambda] = fmincon(f,p0,A,b,Aeq,beq,lb,ub,nonlcon,options); % changed x to p
Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
ndata = length(yData);
yB_Model = zeros(ndata,1);
for i = 1:ndata
F1 = yData(i,1);
x = [1; 0; 0; 390];
x = fsolve(@(x)model(x, F1, p),x);
yB_Model(1) = x(2);
end
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
% plots
% graph 1: experimental data v. model with optimal parameter values
figure;
plot(yData(:,1),yData(:,2),'o',yData(:,1),yModel);
Unrecognized function or variable 'yModel'.
xlim([yData(1,1), yData(idata,1)]);
title('y_B Data v. Flowrate (1)');
xlabel('Flowrate (1) [ m^3 /s ]');
ylabel('Concentration of y_B [ kmol/m^3 ]')
% graph 2: parity plot (yB v. yB data)
figure;
midX = [min(yData(:,2)), max(yData(:,2))];
midY = [min(yData(:,2)), max(yData(:,2))];
plot(yData(:,2),yBModel, 'o', midX, midY);
xlim([min(yData(:,2)) max(yData(:,2))]);
title('Parity Plot (y_B v. y_B Data');
xlabel('Experimental y_B Data [ kmol/m^3 ]');
ylabel('Model Data y_B Data [ kmol/m^3 ]');
function f = obj(p,yData)
% implement your objective function here
ndata = length(yData);
yModel = zeros(ndata,1);
options = optimoptions(@fsolve, 'Display','off');
for i = 1:ndata
F1 = yData(i,1);
x0 = [1; 0; 0; 390];
x = x0;
x = fsolve(@(x)model(x,F1,p),x,options);
yModel(i) = x(2);
end
f = norm(yData(:,2)-yModel).^2;
end
function h = model(x,F1,p) %Run this section to solve for Part B
% Implement the constraints that define the model
Va = 0.08937;
Vb = 0.1018;
Vc = 0.113;
Ya0 = 1;
Yb0 = 0;
Yc0 = 0;
V = 10;
% all constants given by the problem statement
r1 = (p(1)*x(1))/(x(1)*Va + x(2)*Vb + x(3)*Vc);
r2 = (p(2)*x(2))/(x(1)*Va + x(2)*Vb + x(3)*Vc);
% rate equations given by problem - these have our p values included in the
% equations
h = zeros(4,1);
h(1) = ((x(1)*x(4)) + (V*r1)) - Ya0*F1;
h(2) = ((x(2)*x(4)) + (V*(r2 - r1))) - Yb0*F1;
h(3) = ((x(3)*x(4)) - (V*(r2))) - Yc0*F1;
h(4) = x(1) + x(2) + x(3) - Ya0;
% these are our equality constraints
end

Accepted Answer

Dyuman Joshi
Dyuman Joshi on 8 Mar 2024
Moved: Walter Roberson on 9 Mar 2024
Because there is no such variable named yModel before that plot() is called.
You have made a typo, it should be yB_Model.
Also, you are using (atleast) one variable without defining it - idata. Or maybe that is a typo as well.
I suggest you go through your code to check for any such discrepancies.
  2 Comments
Justyn Welsh
Justyn Welsh on 9 Mar 2024
Moved: Walter Roberson on 9 Mar 2024
Thank you! This was resolved.
Justyn Welsh
Justyn Welsh on 9 Mar 2024
Moved: Walter Roberson on 9 Mar 2024
Also, I can't seem to accept your answer? Maybe because you commented, but I would like to give you credit.

Sign in to comment.

More Answers (0)

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!