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

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()

Related

Numerical Integration by Simpsons method

I am trying to solve this integration by simpsons method and plot the result in a figure.The figure is taking only the value of P0= -6 from the for loop. When I set I(K,P) it gives the error:
Attempted to access P0(0); index must be a positive integer or logical
How can I solve it?
alpha = 45;
beta = 185;
gamma_e = 116;
% Gain values
G_ei = -18.96;
G_ee = 18.52;
G_sr = -0.26;
G_rs = 16.92;
G_es = 2.55;
G_re = 4.67;
G_se = 0.73;
G_sn = 2.78;
G_esre = G_es*G_sr*G_re;
G_srs = G_sr*G_rs;
G_ese = G_es*G_se;
G_esn = G_es*G_sn;
t_0 = 0.085; % corticothalamic loop delay in second
r_e = 0.086; % Excitatory axon range in metre
f = linspace(-40,40,500); % f = frequency in Hz
w = 2*pi*f; % angular frequency in radian per second
delt_P = 0.5;
L=zeros(1,500);
Q=repmat(L,1);
P=repmat(L,1);
%%%%%%%%%%%%% integration %%%%%%%%%%%%
a = -80*pi;
b = 80*pi;
n=500;
I=repmat(L,1);
P_initial = repmat(L,1);
P_shift = repmat(L,1);
p = repmat(L,1);
for k = 1:length(w)
for P0 = [6 -6]
L_initial = #(w1) (1-((1i*w1)/alpha))^(-1)*(1-((1i*w1)/beta))^(-1);
Q_initial = #(w1) (1/(r_e^2))*((1-((1i*w1)/gamma_e))^(2) - (1/(1-G_ei*L_initial(w1)))*....
(L_initial(w1)*G_ee + (exp(1i*w1*t_0)*(L_initial(w1)^2*G_ese +L_initial(w1)^3*G_esre))/(1-L_initial(w1)^2*G_srs)));
P_initial = #(w1) (pi/r_e^4)* (abs((L_initial(w1)^2*G_esn)/((1-L_initial(w1)^2*G_srs)*....
(1-G_ei*L_initial(w1)))))^2 * abs((atan2((imag(Q_initial(w1))),(real(Q_initial(w1)))))/imag(Q_initial(w1)));
G = 150*exp(- (f - P0).^2./(2*(delt_P).^2));
P2 = #(w1) G(k) + P_initial(w1);
L_shift = #(w1) (1-((1i*(w(k)-w1))/alpha))^(-1)* (1-((1i*(w(k)-w1))/beta))^(-1);
Q_shift = #(w1) (1/(r_e^2))*((1-((1i*(w(k)-w1))/gamma_e))^(2) - (1/(1-G_ei*L_shift(w1)))*...
(L_shift(w1)*G_ee + (exp(1i*(w(k)-w1)*t_0)*(L_shift(w1)^2*G_ese +L_shift(w1)^3*G_esre))/(1-L_shift(w1)^2*G_srs)));
P_shift = #(w1) (pi/r_e^4)* (abs((L_shift(w1)^2*G_esn)/((1-L_shift(w1)^2*G_srs)*(1-G_ei*L_shift(w1)))))^2 *....
abs((atan2((imag(Q_shift(w1))),(real(Q_shift(w1)))))/imag(Q_shift(w1)));
p = #(w1) P2(w1)*P_shift(w1); % Power spectrum formula for P(w1)*p(w-w1)
I(k) = simprl(p,a,b,n);
end
end
figure(1)
plot(f,I,'r--')
figure(2)
plot(f,G,'k')
At the moment you only use the results for P0 = -6 as you save them in I(k). First you save the result for P0 = 6 later you overwrite it and save the other. The results of P0 = 6are neither used nor saved. If you write your code as follows this will be clarifyied.
for k = 1:length(w)
L_shift = #(w1) (1-((1i*(w(k)-w1))/alpha))^(-1)* (1-((1i*(w(k)-w1))/beta))^(-1);
Q_shift = #(w1) (1/(r_e^2))*((1-((1i*(w(k)-w1))/gamma_e))^(2) - (1/(1-G_ei*L_shift(w1)))*...
(L_shift(w1)*G_ee + (exp(1i*(w(k)-w1)*t_0)*(L_shift(w1)^2*G_ese +L_shift(w1)^3*G_esre))/(1-L_shift(w1)^2*G_srs)));
P_shift = #(w1) (pi/r_e^4)* (abs((L_shift(w1)^2*G_esn)/((1-L_shift(w1)^2*G_srs)*(1-G_ei*L_shift(w1)))))^2 *....
abs((atan2((imag(Q_shift(w1))),(real(Q_shift(w1)))))/imag(Q_shift(w1)));
for P0 = [6 -6]
G = 150*exp(- (f - P0).^2./(2*(delt_P).^2));
P2 = #(w1) G(k) + P_initial(w1);
p = #(w1) P2(w1)*P_shift(w1);
I(k) = simprl(p,a,b,n);
end
end
You can't access I(k,P) as I is an vector not an matrix. However this will give you Index exceeds matrix dimensions. You could save the results for P0 = -6 in one variable and P0 = 6 in the other variable as the results in your code do not depent on each other.

MATLAB - Adaptive Step Size Runge-Kutta

I've programmed in MATLAB an adaptive step size RK4 to solve a system of ODEs. The code runs without error, however it does not produce the desired curve when I try to plot x against y. Instead of being a toroidal shape, I simply get a flat line. This is evident from the fact that r is outputting a constant value. After checking the outputs of each line, they are not outputting constants or errors or inf or NaN, rather they are outputting both a real and imaginary component (complex numbers). I have no idea as to why this is occurring and I believe it to be the source of my trouble.
function AdaptRK4()
parsec = 3.08*10^18;
r_1 = 8.5*1000.0*parsec; % in cm
theta_1 = 0.0;
a = 0.5*r_1;
gam = 1;
grav = 6.6720*10^-8;
amsun = 1.989*10^33;
amg = 1.5d11*amsun;
gm = grav*amg;
u_1 = 20.0*10^5;
v = sqrt(gm/r_1);
time = 0.0;
epsilon = 0.00001;
m1 = 0.5;
m2 = 0.5;
m3 = 0.5;
i = 1;
nsteps = 50000;
deltat = 5.0*10^12;
angmom = r_1*v;
angmom2 = angmom^2.0;
e = -2*10^5.0*gm/r_1+u_1*u_1/2.0+angmom2/(2.0*r_1*r_1);
for i=1:nsteps
deltat = min(deltat,nsteps-time);
fk3_1 = deltat*u_1;
fk4_1 = deltat*(-gm*r_1*r_1^(-gam)/(a+r_1)^(3- gam)+angmom2/(r_1^3.0));
fk5_1 = deltat*(angmom/(r_1^2.0));
r_2 = r_1+fk3_1/4.0;
u_2 = u_1+fk4_1/4.0;
theta_2 = theta_1+fk5_1/4.0;
fk3_2 = deltat*u_2;
fk4_2 = deltat*(-gm*r_2*r_2^(-gam)/(a+r_2)^(3-gam)+angmom2/(r_2^3.0));
fk5_2 = deltat*(angmom/(r_2^2.0));
r_3 = r_1+(3/32)*fk3_1 + (9/32)*fk3_2;
u_3 = u_1+(3/32)*fk4_1 + (9/32)*fk4_2;
theta_3 = theta_1+(3/32)*fk5_1 + (9/32)*fk5_2;
fk3_3 = deltat*u_3;
fk4_3 = deltat*(-gm*r_3*r_3^(-gam)/(a+r_3)^(3-gam)+angmom2/(r_3^3.0));
fk5_3 = deltat*(angmom/(r_3^2.0));
r_4 = r_1+(1932/2197)*fk3_1 - (7200/2197)*fk3_2 + (7296/2197)*fk3_3;
u_4 = u_1+(1932/2197)*fk4_1 - (7200/2197)*fk4_2 + (7296/2197)*fk4_3;
theta_4 = theta_1+(1932/2197)*fk5_1 - (7200/2197)*fk5_2 + (7296/2197)*fk5_3;
fk3_4 = deltat*u_4;
fk4_4 = deltat*(-gm*r_4*r_4^(-gam)/(a+r_4)^(3-gam)+angmom2/(r_4^3.0));
fk5_4 = deltat*(angmom/(r_4^2.0));
r_5 = r_1+(439/216)*fk3_1 - 8*fk3_2 + (3680/513)*fk3_3 - (845/4104)*fk3_4;
u_5 = u_1+(439/216)*fk4_1 - 8*fk4_2 + (3680/513)*fk4_3 - (845/4104)*fk4_4;
theta_5 = theta_1+(439/216)*fk5_1 - 8*fk5_2 + (3680/513)*fk5_3 - (845/4104)*fk5_4;
fk3_5 = deltat*u_5;
fk4_5 = deltat*(-gm*r_5*r_5^(-gam)/(a+r_5)^(3-gam)+angmom2/(r_5^3.0));
fk5_5 = deltat*(angmom/(r_5^2.0));
r_6 = r_1-(8/27)*fk3_1 - 2*fk3_2 - (3544/2565)*fk3_3 + (1859/4104)*fk3_4-(11/40)*fk3_5;
u_6 = u_1-(8/27)*fk4_1 - 2*fk4_2 - (3544/2565)*fk4_3 + (1859/4104)*fk4_4-(11/40)*fk4_5;
theta_6 = theta_1-(8/27)*fk5_1 - 2*fk5_2 - (3544/2565)*fk5_3 + (1859/4104)*fk5_4-(11/40)*fk5_5;
fk3_6 = deltat*u_6;
fk4_6 = deltat*(-gm*r_6*r_6^(-gam)/(a+r_6)^(3-gam)+angmom2/(r_6^3.0));
fk5_6 = deltat*(angmom/(r_6^2.0));
fm3_1 = m1 + 25*fk3_1/216+1408*fk3_3/2565+2197*fk3_4/4104-fk3_5/5;
fm4_1 = m2 + 25*fk4_1/216+1408*fk4_3/2565+2197*fk4_4/4104-fk4_5/5;
fm5_1 = m3 + 25*fk5_1/216+1408*fk5_3/2565+2197*fk5_4/4104-fk5_5/5;
fm3_2 = m1 + 16*fk3_1/135+6656*fk3_3/12825+28561*fk3_4/56430-9*fk3_5/50+2*fk3_6/55;
fm4_2 = m2 + 16*fk4_1/135+6656*fk4_3/12825+28561*fk4_4/56430-9*fk4_5/50+2*fk4_6/55;
fm5_2 = m3 + 16*fk5_1/135+6656*fk5_3/12825+28561*fk5_4/56430-9*fk5_5/50+2*fk5_6/55;
R3 = abs(fm3_1-fm3_2)/deltat;
R4 = abs(fm4_1-fm4_2)/deltat;
R5 = abs(fm5_1-fm5_2)/deltat;
err3 = 0.84*(epsilon/R3)^(1/4);
err4 = 0.84*(epsilon/R4)^(1/4);
err5 = 0.84*(epsilon/R5)^(1/4);
if R3<= epsilon
time = time+deltat;
fm3 = fm3_1;
i = i+1;
deltat = err3*deltat;
end
if R4<= epsilon
time = time+deltat;
fm4 = fm4_1;
i = i+1;
deltat = err4*deltat;
end
if R5<= epsilon
time = time+deltat;
fm5 = fm5_1;
i = i+1;
deltat = err5*deltat;
end
e=2*gm^2.0/(2*angmom2);
ecc=(1.0+(2.0*e*angmom2)/(gm^2.0))^0.5;
x(i)=r_1*cos(theta_1)/(1000.0*parsec);
y(i)=r_1*sin(theta_1)/(1000.0*parsec);
time=time+deltat;
r(i)=r_1;
time1(i)=time;
end
figure()
plot(x,y, '-k');
TeXString = title('Plot of Orbit in Gamma Potential Obtained Using RK4')
xlabel('x')
ylabel('y')
You are getting complex values because at some point npts - time < 0. You may want to print out the values of deltat to check the error.
Also, your code doesn't seem to take into account the case when the error estimate is larger than your tolerance. When your error estimate is greater than your tolerance you have to:
Shift back the time AND solution
calculate a new step-size based on a formula, and
recalculate your solution and error estimate.
The fact that you don't know how many iterations you will have to go through makes the use of a for-loop for adaptive runge Kutta a bit awkward. I suggest using a while loop instead.
You are using "i" in your code. "i" returns the basic imaginary unit. "i" is equivalent to sqrt(-1). Try to use another identifier in your loops and only use "i" or "j" in calculations where complex numbers are involved.

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.

The method of characteristics for two dimensional advection equation

Given the following function to solve the two-dimensional advection equation in a rectangle:
function qnew = SemiLagrAdvect(u,v,q,qS,qN,qW,qE)
global N M
global dx dy
global dt Re
u = reshape(u,N,M);
v = reshape(v,N,M);
q = reshape(q,N,M);
%...embedding
qq = zeros(N+2,M+2);
qq(2:N+1,2:M+1) = q;
%...set the ghost values (four edges)
qq(1,2:M+1) = 2*qW-qq(2,2:M+1);
qq(N+2,2:M+1) = 2*qE-qq(N+1,2:M+1);
qq(2:N+1,1) = 2*qS-qq(2:N+1,2);
qq(2:N+1,M+2) = 2*qN-qq(2:N+1,M+1);
%...set the ghost values (four corners)
qq(1,1) = -qq(2,2);
qq(N+2,1) = -qq(N+1,2);
qq(N+2,M+2) = -qq(N+1,M+1);
qq(1,M+2) = -qq(2,M+1);
q1 = qq(2:N+1,2:M+1);
q2p = qq(3:N+2,2:M+1);
q2m = qq(1:N,2:M+1);
q3p = qq(2:N+1,3:M+2);
q3m = qq(2:N+1,1:M);
q4pp = qq(3:N+2,3:M+2);
q4mm = qq(1:N,1:M);
q4pm = qq(3:N+2,1:M);
q4mp = qq(1:N,3:M+2);
xi = -u*dt/dx;
eta = -v*dt/dy;
Q2 = q2p.*(xi>0) + q2m.*(xi<0);
Q3 = q3p.*(eta>0) + q3m.*(eta<0);
Q4 = q4pp.*((xi>0) & (eta>0)) + q4mm.*((xi<0) & (eta<0)) + ...
q4pm.*((xi>0) & (eta<0)) + q4mp.*((xi<0) & (eta>0));
qnew = (1-abs(xi)).*(1-abs(eta)).*q1 + ...
abs(xi).*(1-abs(eta)).*Q2 + ...
abs(eta).*(1-abs(xi)).*Q3 + ...
abs(xi).*abs(eta).*Q4;
qnew = qnew(:);
Having elementary knowledge in MATLAB, how can I modify it to solve the equation in a composite domain?
you need to use continuity on the interface.Depending on your method.
In the method of characteristics the interface is part of the initial curve.
S
I'm

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.