MLR mod not found - mod

I love MLR! In the code below I am comparing the performance of four classifiers. I am getting some weird errors when I run the following code using the PIMA Indian Diabetes data:
library(mlbench)
library(caret)
library(randomForest)
data(PimaIndiansDiabetes)
data2<-data
## Define the task
Class.task = makeClassifTask(id = "USUBJID", data = data2, target = "Class", positive ="B")
Class.task = makeClassifTask( data = PimaIndiansDiabetes, target = "diabetes", positive ="pos")
fv = generateFilterValuesData(Class.task, method = "mrmr")
plotFilterValues(fv)
filtered.task = filterFeatures(Class.task, fval = fv, threshold = -.2)
#filtered.task = Class.task
n = getTaskSize(filtered.task)
train.set = sample(n, size = round(2/3 * n))
test.set = setdiff(seq_len(n), train.set)
lrn1 = makeLearner("classif.lda", predict.type = "prob")
mod1 = train(lrn1, filtered.task, subset = train.set)
pred1 = predict(mod1, task = filtered.task, subset = test.set)
lrn2 = makeLearner("classif.ksvm", predict.type = "prob")
mod2 = train(lrn2, filtered.task, subset = train.set)
pred2 = predict(mod2, task = filtered.task, subset = test.set)
lrn3 = makeLearner("classif.randomForest", predict.type = "prob")
mod3 = train(lrn3, Class.task, subset = train.set)
pred3 = predict(mod3, task = Class.task, subset = test.set)
lrn5 = makeLearner("classif.xgboost", predict.type = "prob")
mod5 = train(lrn5, Class.task, subset = train.set)
pred5 = predict(mod5, task = Class.task, subset = test.set)
### Tune wrapper for ksvm
rdesc.inner = makeResampleDesc("Holdout")
ms = list(auc, mmce)
ps = makeParamSet(
makeDiscreteParam("C", 2^(-1:1))
)
ctrl = makeTuneControlGrid()
lrn2 = makeTuneWrapper(lrn2, rdesc.inner,ms, ps, ctrl, show.info = FALSE)
lrns = list(lrn1, lrn2,lrn3,lrn5)
rdesc.outer = makeResampleDesc("CV", iters = 5)
bmr = benchmark(lrns, tasks = filtered.task, resampling = rdesc.outer, measures = ms, show.info = FALSE)
bmr
The errors I am obtaining are:
Error in unique.default(x, nmax = nmax) :
unique() applies only to vectors
> pred1 = predict(mod1, task = filtered.task, subset = test.set)
Error in predict(mod1, task = filtered.task, subset = test.set) :
object 'mod1' not found
> lrn2 = makeLearner("classif.ksvm", predict.type = "prob")
> mod2 = train(lrn2, filtered.task, subset = train.set)
Error in unique.default(x, nmax = nmax) :
unique() applies only to vectors
> pred2 = predict(mod2, task = filtered.task, subset = test.set)
Error in predict(mod2, task = filtered.task, subset = test.set) :
object 'mod2' not found
> lrn3 = makeLearner("classif.randomForest", predict.type = "prob")
> mod3 = train(lrn3, Class.task, subset = train.set)
Error in unique.default(x, nmax = nmax) :
unique() applies only to vectors
> pred3 = predict(mod3, task = Class.task, subset = test.set)
Error in predict(mod3, task = Class.task, subset = test.set) :
object 'mod3' not found
>
> lrn5 = makeLearner("classif.xgboost", predict.type = "prob")
> mod5 = train(lrn5, Class.task, subset = train.set)
Error in unique.default(x, nmax = nmax) :
unique() applies only to vectors
> pred5 = predict(mod5, task = Class.task, subset = test.set)
Error in predict(mod5, task = Class.task, subset = test.set) :
I do get the performance results.. Any ideas on what I am doing wrong? Thank you!!!

The problem is that you're loading mlr before caret -- both have train functions and one shadows the other depending on which you load first. You need to load the mlr package last (and you shouldn't need caret at all here).
Edit: Complete working code
library(mlbench)
library(mlr)
data(PimaIndiansDiabetes)
Class.task = makeClassifTask( data = PimaIndiansDiabetes, target = "diabetes", positive ="pos")
fv = generateFilterValuesData(Class.task, method = "mrmr")
plotFilterValues(fv)
filtered.task = filterFeatures(Class.task, fval = fv, threshold = -.2)
#filtered.task = Class.task
n = getTaskSize(filtered.task)
train.set = sample(n, size = round(2/3 * n))
test.set = setdiff(seq_len(n), train.set)
lrn1 = makeLearner("classif.lda", predict.type = "prob")
mod1 = train(lrn1, filtered.task, subset = train.set)
pred1 = predict(mod1, task = filtered.task, subset = test.set)
lrn2 = makeLearner("classif.ksvm", predict.type = "prob")
mod2 = train(lrn2, filtered.task, subset = train.set)
pred2 = predict(mod2, task = filtered.task, subset = test.set)
lrn3 = makeLearner("classif.randomForest", predict.type = "prob")
mod3 = train(lrn3, Class.task, subset = train.set)
pred3 = predict(mod3, task = Class.task, subset = test.set)
lrn5 = makeLearner("classif.xgboost", predict.type = "prob")
mod5 = train(lrn5, Class.task, subset = train.set)
pred5 = predict(mod5, task = Class.task, subset = test.set)
### Tune wrapper for ksvm
rdesc.inner = makeResampleDesc("Holdout")
ms = list(auc, mmce)
ps = makeParamSet(
makeDiscreteParam("C", 2^(-1:1))
)
ctrl = makeTuneControlGrid()
lrn2 = makeTuneWrapper(lrn2, rdesc.inner,ms, ps, ctrl, show.info = FALSE)
lrns = list(lrn1, lrn2,lrn3,lrn5)
rdesc.outer = makeResampleDesc("CV", iters = 5)
bmr = benchmark(lrns, tasks = filtered.task, resampling = rdesc.outer, measures = ms, show.info = FALSE)
bmr

Related

Dyn.Opt problem solution doesn't converge on GEKKO on varying no. of time pts or values of maniplatd var or colc. nodes

I'm solving a Dynamic Optimization problem on gekko but the solution doesn't converge if I vary the number of time points or initial/lb/ub values for manipulated variable or change the collocation nodes. What could be the issue?
Just changed the values mentioned above, and on some combinations, the solution doesn't converge at all, and the following error message appears:
raise Exception(response)
Exception: #error: Solution Not Found
Code:
from gekko import GEKKO
import numpy as np
import matplotlib.pyplot as plt
m = GEKKO()
nt = 101 # no. of time steps
m.time = np.linspace(0,147,nt)
k17 = 0.0508
k26 = 1
k27 = 0.0577
k28 = 0.0104
k32 = 2
k33 = 2
k37 = 0.0016
k38 = 0.0107
k47 = 0.006
k48 = 0.072
k53 = 2
k57 = 0.0201
k58 = 0.082
k61 = 2
k62 = 2
k63 = 2
k72 = 2
k77 = 0.0133
k78 = 0.011
k87 = 0.081
k88 = 0.0148
kd = 0.06
w = 0.043
s1 = m.Var(value=0,lb=0)
s2 = m.Var(value=3,lb=0)
s3 = m.Var(value=0.01,lb=0)
s4 = m.Var(value=0.46,lb=0)
s5 = m.Var(value=0.27,lb=0)
p1 = m.Var(value=0.51,lb=0)
p2 = m.Var(value=0.33,lb=0)
p3 = m.Var(value=0.3,lb=0)
p4 = m.Var(value=0.34e-4,lb=0)
p5 = m.Var(value=2.01,lb=0)
p6 = m.Var(value=0.05,lb=0)
X = m.Var(value=0.09,lb=0)
Xd = m.Var(value=0.02,lb=0)
V = m.Var(value=5,lb=0,ub=10)
u = m.MV(value=0.05,lb=0,ub=0.1) # manipulated variable
u.STATUS = 1
u.DCOST = 0
f1 = (8.443e-4)*X*s1/(8.989e5 + s1)
f2 = (2.481e6)*X*s1*s3/((6.495e4 + s1)*(7.076e2 + s3))
f3 = (3.968e5)*X*s1*s3/((3.723e4 + s1)*(2.782e3 + s3))
f4 = (1.09e2)*X*s3/(0.019+s3)
f5 = 7.283*X*s4/(1.92e3 + s4)
f6 = (3.337e5)*X*s2*s5/((2.719e4 + s2)*(4.488e4 + s5))
f7 = (3.977e3)*X*s2/(9.324e3 + s2)
f8 = (6.697e-6)*X*s2/(0.537+s2)
f9 = (3.261e4)*X*s2/(6.683e5 + s2)
P = np.zeros(nt)
P[-1] = 1.0
final = m.Param(value=P)
# Equations
m.Equation(V.dt()==u)
m.Equation(s1.dt()==-f1-f2-f3-k17*f7+(2-s1)*u/V)
m.Equation(s2.dt()==-f6-k27*f7-k28*f8-f9-s2*u/V)
m.Equation(s3.dt()==-k32*f2-k33*f3-f4+f6-k37*f7-k38*f8+f9-s3*u/V)
m.Equation(s4.dt()==-f5+f6-k47*f7-k48*f8-s4*u/V)
m.Equation(s5.dt()==k53*f3+f5-f6-k57*f7-k58*f8-s5*u/V)
m.Equation(p1.dt()==k61*f1+k62*f2+k63*f3-p1*u/V)
m.Equation(p2.dt()==k72*f2-k77*f7-k78*f8-p2*u/V)
m.Equation(p3.dt()==f4-k87*f7-k88*f8-p3*u/V)
m.Equation(p4.dt()==f8-p4*u/V)
m.Equation(p5.dt()==f7-p5*u/V)
m.Equation(p6.dt()==f5+f9-p6*u/V)
m.Equation(X.dt()==w*X-kd*X*Xd-X*u/V)
m.Equation(Xd.dt()==kd*X*Xd-Xd*u/V)
m.Obj(-final*V*p4)
m.options.IMODE = 6
m.options.NODES = 4
#m.options.COLDSTART=2
#m.options.MAX_ITER=1000
m.solve(disp=True)
p4_ = np.multiply(p4.value,1000)
plt.figure(1)
plt.subplot(2,1,1)
plt.plot(m.time,u.value,'r-')
plt.subplot(2,1,2)
plt.plot(m.time,p4_,'b--')
There are several reasons that the solver fails to reach a solution with Solution Not Found. Here are some of the reasons and things to try:
If the solver reports No Feasible Solution then the bounds are too restrictive. Widen the variable bounds until a solution is obtained.
If the solver reaches the maximum iterations, try changing the solver with m.options.SOLVER=1 or letting the solver continue for more iterations with m.options.MAX_ITER=500.
If the solver stops with the message Unbounded Solution then try setting bounds on the MV values.
Try initialization with no degrees of freedom by setting {FV}.STATUS=0 and {MV}.STATUS=0). The simulation should have the same number of equations and variables as a square problem. Switching to m.options.IMODE=7 is a sequential simulation and can also help with the initialization. Setting m.options.COLDSTART=1 or m.options.COLDSTART=2 can help find an initial solution that helps the optimization. Additional initialization resources are available in the Dynamic Optimization course.
Please post a copy of your code for more specific suggestions. There are several Dynamic Optimization Benchmark problems posted to the Dynamic Optimization Course.
Response to Edit
Try setting a smaller time scale until the problem converges. Here is a successful solution with a final time of 0.08.
m.time = np.linspace(0,0.08,11)
The solver does not find a solution after this time point. It reports an infeasible solution. One cause of this may be variable p4 that makes it infeasible because of the lower bound of zero.
Here is the diagnostic code with the plots:
from gekko import GEKKO
import numpy as np
import matplotlib.pyplot as plt
m = GEKKO()
m.time = np.linspace(0,0.08,11)
nt = len(m.time)
k17 = 0.0508
k26 = 1
k27 = 0.0577
k28 = 0.0104
k32 = 2
k33 = 2
k37 = 0.0016
k38 = 0.0107
k47 = 0.006
k48 = 0.072
k53 = 2
k57 = 0.0201
k58 = 0.082
k61 = 2
k62 = 2
k63 = 2
k72 = 2
k77 = 0.0133
k78 = 0.011
k87 = 0.081
k88 = 0.0148
kd = 0.06
w = 0.043
s1 = m.Var(value=0,lb=0)
s2 = m.Var(value=3,lb=0)
s3 = m.Var(value=0.01,lb=0)
s4 = m.Var(value=0.46,lb=0)
s5 = m.Var(value=0.27,lb=0)
p1 = m.Var(value=0.51,lb=0)
p2 = m.Var(value=0.33,lb=0)
p3 = m.Var(value=0.3,lb=0)
p4 = m.Var(value=0.34e-4,lb=0)
p5 = m.Var(value=2.01,lb=0)
p6 = m.Var(value=0.05,lb=0)
X = m.Var(value=0.09,lb=0)
Xd = m.Var(value=0.02,lb=0)
V = m.Var(value=5,lb=0.01)
u = m.MV(value=0.05,lb=0,ub=0.1) # manipulated variable
u.STATUS = 1
u.DCOST = 0
f1 = m.Intermediate((8.443e-4)*X*s1/(8.989e5 + s1))
f2 = m.Intermediate((2.481e6)*X*s1*s3/((6.495e4 + s1)*(7.076e2 + s3)))
f3 = m.Intermediate((3.968e5)*X*s1*s3/((3.723e4 + s1)*(2.782e3 + s3)))
f4 = m.Intermediate((1.09e2)*X*s3/(0.019+s3))
f5 = m.Intermediate(7.283*X*s4/(1.92e3 + s4))
f6 = m.Intermediate((3.337e5)*X*s2*s5/((2.719e4 + s2)*(4.488e4 + s5)))
f7 = m.Intermediate((3.977e3)*X*s2/(9.324e3 + s2))
f8 = m.Intermediate((6.697e-6)*X*s2/(0.537+s2))
f9 = m.Intermediate((3.261e4)*X*s2/(6.683e5 + s2))
P = np.zeros(nt)
P[-1] = 1.0
final = m.Param(value=P)
# Equations
m.Equation(V.dt()==u)
m.Equation(s1.dt()==-f1-f2-f3-k17*f7+(2-s1)*u/V)
m.Equation(s2.dt()==-f6-k27*f7-k28*f8-f9-s2*u/V)
m.Equation(s3.dt()==-k32*f2-k33*f3-f4+f6-k37*f7-k38*f8+f9-s3*u/V)
m.Equation(s4.dt()==-f5+f6-k47*f7-k48*f8-s4*u/V)
m.Equation(s5.dt()==k53*f3+f5-f6-k57*f7-k58*f8-s5*u/V)
m.Equation(p1.dt()==k61*f1+k62*f2+k63*f3-p1*u/V)
m.Equation(p2.dt()==k72*f2-k77*f7-k78*f8-p2*u/V)
m.Equation(p3.dt()==f4-k87*f7-k88*f8-p3*u/V)
m.Equation(p4.dt()==f8-p4*u/V)
m.Equation(p5.dt()==f7-p5*u/V)
m.Equation(p6.dt()==f5+f9-p6*u/V)
m.Equation(X.dt()==w*X-kd*X*Xd-X*u/V)
m.Equation(Xd.dt()==kd*X*Xd-Xd*u/V)
m.Maximize(final*V*p4)
m.options.IMODE = 6
m.options.NODES = 3
m.options.SOLVER = 1
m.options.COLDSTART=0
#m.options.MAX_ITER=1000
m.solve(disp=True)
p4_ = np.multiply(p4.value,1000)
plt.figure(1)
plt.subplot(2,1,1)
plt.plot(m.time,u.value,'r-')
plt.subplot(2,1,2)
plt.plot(m.time,p4_,'b--')
plt.figure(2)
plt.plot(m.time,s1,label='s1')
plt.plot(m.time,s2,label='s2')
plt.plot(m.time,s3,label='s3')
plt.plot(m.time,s4,label='s4')
plt.plot(m.time,s5,label='s5')
plt.legend()
plt.figure(3)
plt.plot(m.time,p1,label='p1')
plt.plot(m.time,p2,label='p2')
plt.plot(m.time,p3,label='p3')
plt.plot(m.time,p4,label='p4')
plt.plot(m.time,p5,label='p5')
plt.plot(m.time,p6,label='p6')
plt.legend()
plt.figure(4)
plt.subplot(3,1,1)
plt.plot(m.time,s1,label='s1')
plt.legend()
plt.subplot(3,1,2)
plt.plot(m.time,s3,label='s3')
plt.legend()
plt.subplot(3,1,3)
plt.plot(m.time,p4,label='p4')
plt.legend()
plt.show()

How do I resolve error "Operator '>' is not supported for operands of type 'function_handle'" in Matlab?

The purpose of this iterative function is to update two parameters of my function to keep output values within a desired range (19 to 21)
Here is what I have so far
for i = 2:tw
T1f(i) = dT1(i,T1(i-1),T2(i-1))+T1(i-1);
T2f(i) = dT2(i,T1(i-1),T2(i-1))+T2(i-1);
if ((T1f(i) > 21) | (T2f(i) > 21)) && dT1f > 0
k2f(i:(i+1799)) = k2f(i)-2;
Qfin(i:(i+1799)) = Qfin(i)-500;
N = N+1;
elseif ((19 > T1f(i)) | (19 > T2f(i))) && 0 > dT1f
k2f(i:i+1799) = k2f(i)+2;
Qfin(i:(i+1799)) = Qfin(i)+500;
N = N+1;
end
k2f = max(k2f,10);
k2f = min(k2f,1);
Qfin = max(Qfin,2500);
Qfin = max(Qfin,0);
end
I keep receiving the error Operator '>' is not supported for operands of type 'function_handle for the elseif line, but not at the if line. Why is this and how to I remedy it? Any suggestions about a better way to acheive this would also be appreciated.
Full file will be below.
k1 = 0.2; %W/m^2
k2 = 1750; % W/m^2, constant
k3 = 0.5; %W/m^2
Ain = 0.5; %m^2
Cair = 1005; %J/kg*K
Pair = 1.2; %kg/m^3
Qin = 2500; %W/m^2 constant
L = 5; %m
W = 6; %m
H = 3; %m H1 = H2 = H
T1o = 5; %C
T2o = 7; %C
N = 1;
% Calculating Areas and volumes
Awalls = 2*((L*H)+(W*H)); %m^2
Aceiling = W*L; %m^2
Aroof = sqrt((W*L)^2+(L*H*2)^2)+W*H; %
Vup = W*H*L/2; %m^3
Vdown = W*H*L; %m^3
%Initializing Arrays and Values
tw = 86400*2; %number of seconds in two days
t = [1:1:tw]; %array with integers 1-tw
n = length(t);
T = zeros(1,n);
T1 = zeros(1,n);
T2 = zeros(1,n);
T1f = zeros(1,n);
T2f = zeros(1,n);
T1(1) = T1o;
T2(1) = T2o;
T1f(1) = T1o;
T2f(1) = T2o;
%% Setting Q values
Qfin = zeros(1,n); %Qin for fluctuation
Qfin(1) = 2500;
%% Setting k2 values
k2f = zeros(1,n); %k3 for fluctuation
kf(1) = 10;
%% Defining Functions
Tout = #(t,T) -10*sin(2*pi*t/86400);
%Constant k2/Qin
dT1 = #(t,T1,T2) ((Ain*Qin)-(Aceiling*k2*(T1-T2-5))-(Awalls*k1*(T1-
Tout(t))))/(Cair*Pair*Vdown);
dT2 = #(t,T1,T2) ((Aceiling*k2*(T1-T2-5))-(Aroof*k3*(T2-Tout(t))))/(Cair*Pair*Vup);
%Fluctuating k2/Qin
dT1f = #(t,T1,T2) ((Ain*Qfin(t))-(Aceiling*k2f(t)*(T1-T2-5))-(Awalls*k1*(T1-
Tout(t))))/(Cair*Pair*Vdown);
dT2f = #(t,T1,T2) ((Aceiling*k2f(t)*(T1-T2-5))-(Aroof*k3*(T2-Tout(t))))/(Cair*Pair*Vdown);
%% Iterate for constant k2/Qin with Euler's
for i = 2:tw
T1(i) = dT1(i,T1(i-1),T2(i-1))+T1(i-1);
T2(i) = dT2(i,T1(i-1),T2(i-1))+T2(i-1);
end
%% Iteratre for fluctuation
for i = 2:tw
T1f(i) = dT1(i,T1(i-1),T2(i-1))+T1(i-1);
T2f(i) = dT2(i,T1(i-1),T2(i-1))+T2(i-1);
if ((T1f(i) > 21) | (T2f(i) > 21)) && dT1f > 0
k2f(i:(i+1799)) = k2f(i)-2;
Qfin(i:(i+1799)) = Qfin(i)-500;
N = N+1;
elseif ((19 > T1f(i)) | (19 > T2f(i))) && 0 > dT1f
k2f(i:i+1799) = k2f(i)+2;
Qfin(i:(i+1799)) = Qfin(i)+500;
N = N+1;
end
k2f = max(k2f,10);
k2f = min(k2f,1);
Qfin = max(Qfin,2500);
Qfin = max(Qfin,0);
end

Order of complexity of a matrix (matlab)

I need some help answering this question : How do I find the complexity of the matrix B and f knowing that I have this code :
d = 123456;
randn('state',d); rand('state',d);
n = 1000; A = diag(1+rand(1,n));
k = 5500; r = randperm(n*n,k);
A(r) = A(r) + rand(1,k);
L = tril(A); U = A-L;
x = rand(n,1);
b = A*x;
B = -L\U; f = L\b;
xit = zeros(n,1);
nit = 100;
res = zeros(nit,1);
incr = zeros(nit,1);
err = zeros(nit,1);
for it = 1:nit,
xit0 = xit;
xit = B*xit+f;
res(it) = norm(b-A*xit);
incr(it) = norm(xit-xit0);
err(it) = norm(xit-x);
end
Thanks in advance

Error: In assignment A(I) = B, the number of elements in B and I must be the same

I'm stuck on K2 as it brought up this error:
In an assignment A(I) = B, the number of elements in B and
I must be the same.
I ran the debugger and I found out that J4 alone was a vector while other variables were all scalar.
How can I resolve this error to have the plot?
Here is the code that I ran.
h1 = 1*10^-6;
h2 = (10:10:1500)*10^-6;
a = 62.5*10^-6;
b = a+h1;
c = b+h2;
alpha_1 = 0.55*10^-6;
alpha_2 = 17.2*10^-6;
alpha_3 = 14.2*10^-6;
zeta = 6.3*10^-6;
P11 = 0.121;
P12 = 0.27;
neff = 1.456;
U1 = 0.17;
U2 = 0.32;
U3 = 0.31;
E1 = 0.74*10^11;
E2 = 1.08*10^11;
E3 = 1.96*10^11;
n = 1;
while(n<=150)
J1(n) = E2*(b^2-a^2)*(1-U1)-E1*a^2*(1-U2)-E1*b^2*(1+U2);
J2(n) = 2*E1*b^2;
J3(n) = E1*E2*(b^2-a^2)*(alpha_2 - alpha_1);
J4(n) = 2*E3*(c(n)^2-b^2)*a^2;
J5(n) = E2*(b^2-a^2)*(1-U3)*b^2+E2*(b^2-a^2)*(1+U3)*c(n)^2-E3*(c(n)^2-b^2)*(1+U2)*a^2-E3*(c(n)^2-b^2)*(1-U2)*b^2;
J6(n) = E2*E3*(c(n)^2 - b^2)*(b^2-a^2)*(alpha_2-alpha_3);
K1(n) = ((alpha_3-alpha_1)*E3*(c(n)^2-b^2)+(alpha_2-alpha_1)*E2*(b^2-a^2))/(E1*a^2+E2*(b^2-a^2)+E3*(c(n)^2-b^2));
K2(n) = (J2*J6-J3*J5)/(J2*J4-J1*J5);
Sr(n) = (neff^2/2)*(P11+P12)*(((1-U1)*K2/E1)-U1*K1);
Sz(n) = (1+P12)*(K1-(2*U2*K2/E1));
St(n) = alpha_1+zeta;
Km(n) = St+Sz+Sr;
n=n+1;
end
plot(h2,Km)
To recap what was already said in one answer, here's how I would modify the code:
h1 = 1e-6;
h2 = (10:10:1500)*1e-6;
a = 62.5*1e-6;
b = a+h1;
c = b+h2;
alpha_1 = 0.55*1e-6;
alpha_2 = 17.2*1e-6;
alpha_3 = 14.2*1e-6;
zeta = 6.3*1e-6;
P11 = 0.121;
P12 = 0.27;
neff = 1.456;
U1 = 0.17;
U2 = 0.32;
U3 = 0.31;
E1 = 0.74*1e11;
E2 = 1.08*1e11;
E3 = 1.96*1e11;
% pre-allocate variables
J1 = zeros(size(h2));
J2 = zeros(size(h2));
J3 = zeros(size(h2));
J4 = zeros(size(h2));
J5 = zeros(size(h2));
J6 = zeros(size(h2));
K1 = zeros(size(h2));
K2 = zeros(size(h2));
Sr = zeros(size(h2));
Sz = zeros(size(h2));
for n=1:length(h2)
J1(n) = E2*(b^2-a^2)*(1-U1)-E1*a^2*(1-U2)-E1*b^2*(1+U2);
J2(n) = 2*E1*b^2;
J3(n) = E1*E2*(b^2-a^2)*(alpha_2 - alpha_1);
J4(n) = 2*E3*(c(n)^2-b^2)*a^2;
J5(n) = E2*(b^2-a^2)*(1-U3)*b^2+E2*(b^2-a^2)*(1+U3)*c(n)^2-E3*(c(n)^2-b^2)*(1+U2)*a^2-E3*(c(n)^2-b^2)*(1-U2)*b^2;
J6(n) = E2*E3*(c(n)^2 - b^2)*(b^2-a^2)*(alpha_2-alpha_3);
K1(n) = ((alpha_3-alpha_1)*E3*(c(n)^2-b^2)+(alpha_2-alpha_1)*E2*(b^2-a^2))/(E1*a^2+E2*(b^2-a^2)+E3*(c(n)^2-b^2));
K2(n) = (J2(n)*J6(n)-J3(n)*J5(n))/(J2(n)*J4(n)-J1(n)*J5(n));
Sr(n) = (neff^2/2)*(P11+P12)*(((1-U1)*K2(n)/E1)-U1*K1(n));
Sz(n) = (1+P12)*(K1(n)-(2*U2*K2(n)/E1));
end
St = alpha_1+zeta;
Km = Sz+Sr+St;
plot(h2,Km)
Notes:
I have used a for loop to ensure the vector lengths are consistent with h2
I have pre-allocated the variables for speed
I have added various (n) to K1, K2, J1, J2, etc... in the equations to have only scalar operations
I have moved stuff out of the for loop that didn't need to be there
This gives the following plot (in Octave)
I ran your code and I found out that the dimensions of the vectors used to compute K2 are not compatible, e.g. each of J2 and J6 is a 1-row by 2-columns vector and you cannot multiply those. This also applies for the other multiplications. Depending on what you want to compute, you should transpose either one of them in each multiplication.
FYI: J.' is the transposed version of J in MATLAB.

Code not working - getting a figure with no plot

Why is this simple code not working properly? I have dozens of other more complex codes based on the same template as this one, and they run faster as well, but it's just not working. I have spent hours trying to fix this.
I should have size(y) = 1001 x 11 and a graph, not just a point. Also, the warning
Failure at t=1.194435e+02. Unable to meet integration tolerances without reducing the step size below the smallest value allowed (2.273737e-13) at time t
doesn't shed light on this.
Here is the script AB.m:
global Q q l v1 v2 v3 v4
v3 = 0.00086;
v4 = 0.000086;
D0 = 0.01;
Q = 0.01;
q = 0.001;
l = 0.001;
v1 = 0.999;
v2 = 0.999;
K0 = 1;
P0 = 1;
S10 = 100;
y0 = [S10 zeros(1,7) K0 P0 D0];
tf = 1e10;
tvec = 0:tf/1e3:tf;
options = odeset('RelTol',1e-4);
[t,y] = ode15s(#ABEqns,tvec,y0,options);
S1 = y(:,1);
S2 = y(:,2);
KS1 = y(:,3);
PS2 = y(:,4);
DS1 = y(:,5);
DS2 = y(:,6);
DKS1 = y(:,7);
DPS2 = y(:,8);
K = y(:,9);
P = y(:,10);
D = y(:,11);
Stot = S1+S2+KS1+PS2+DS1+DS2+DKS1+DPS2;
plot(t,Stot,'k','LineSmoothing','on')
axis([0 tf 0 1.1*max(Stot)])
xlabel('Time (s)')
hold on
Here is the function ABEqns.m:
function ydot = ABEqns(t,y)
global Q q l v1 v2 v3 v4
S1 = y(1);
S2 = y(2);
KS1 = y(3);
PS2 = y(4);
DS1 = y(5);
DS2 = y(6);
DKS1 = y(7);
DPS2 = y(8);
K = y(9);
P = y(10);
D = y(11);
ydot = zeros(11,1);
ydot(1) = Q+l*(DS1+KS1)-q*S1*(D+K)+v2*PS2;
ydot(2) = l*(DS2+PS2)-q*S2*(D+P)+v1*KS1;
ydot(3) = l*(DKS1-KS1)+q*(K*S1-D*KS1);
ydot(4) = l*(DPS2-PS2)+q*(P*S2-D*PS2);
ydot(5) = q*D*S1-(l+v3)*DS1;
ydot(6) = q*D*S2-(l+v4)*DS2;
ydot(7) = q*D*KS1-(l+v3)*DKS1;
ydot(8) = q*D*PS2-(l+v4)*DPS2;
ydot(9) = (l+v1)*KS1-q*K*S1+v3*DKS1;
ydot(10) = (l+v2)*PS2-q*P*S2+v4*DPS2;
ydot(11) = (l+v3)*(DS1+DKS1)+(l+v4)*(DS2+DPS2)-q*D*(S1+S2+KS1+PS2);
end
Thanks for any help.