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.
Related
I'm trying to solve a non-linear system of equations using Newton-Raphson method. The problem is in section 2.1 from this paper. It goes only up to the second iteration, and I got incorrect answer.
I used these values for iN and VN (N=1,2,3,4,5):
V1 = 1.00400400e+00;
V2 = 1.02598500e+00;
V3 = 1.05325500e+00;
V4 = 1.08812300e+00;
V5 = 1.13388700e+00;
i1 = -2.40036700e-02;
i2 = -3.59849700e-02;
i3 = -5.32552100e-02;
i4 = -7.81225600e-02;
i5 = -1.13887200e-01;
I used:
a = Iph,
b = I0,
f = n,
c = Rs,
d = Rsh,
Vth = 0.0258
Here's my code:
clear all; clc;
N = 20;
tErr = 1e-6;
syms a b c d f
V1 = 1.00400400e+00; V2 = 1.02598500e+00; V3 = 1.05325500e+00; V4 = 1.08812300e+00; V5 = 1.13388700e+00; i1 = -2.40036700e-02; i2 = -3.59849700e-02; i3 = -5.32552100e-02; i4 = -7.81225600e-02 ; i5 = -1.13887200e-01;
m1 = d*(-i1 + a + b)/(0.0258*f); m2 = d*(-i2 + a + b)/(0.0258*f); m3 = d*(-i3 + a + b)/(0.0258*f); m4 = d*(-i4 + a + b)/(0.0258*f);
m5 = d*(-i5 + a + b)/(0.0258*f); n = .0258*f; k = b*d/n;
f1 = -i1*c-i1*d+a*d-n*lambertw(b*d*exp(m1)/n)-V1;
f2 = -i2*c-i2*d+a*d-n*lambertw(b*d*exp(m2)/n)-V2;
f3 = -i3*c-i3*d+a*d-n*lambertw(b*d*exp(m3)/n)-V3;
f4 = -i4*c-i4*d+a*d-n*lambertw(b*d*exp(m4)/n)-V4;
f5 = -i5*c-i5*d+a*d-n*lambertw(b*d*exp(m5)/n)-V5;
fs = [f1; f2; f3; f4; f5];
xs = [a; b; f; c; d];
jac = [d/lambertw(b*d*exp(m1)/n), -(n*lambertw(b*d*exp(m1)/n)-b*d)/(b*(1+lambertw(b*d*exp(m1)/n))), lambertw(b*d*exp(m1)/n)*(-n*lambertw(b*d*exp(m1)/n)-i1*d+a*d+b*d)/(f*(1+lambertw(b*d*exp(m1)/n))), -i1, -(n*lambertw(b*d*exp(m1)/n)+i1*d-a*d-b*d)/((1+lambertw(b*d*exp(m1)/n))*d);
d/lambertw(b*d*exp(m2)/n), -(n*lambertw(b*d*exp(m2)/n)-b*d)/(b*(1+lambertw(b*d*exp(m2)/n))), lambertw(b*d*exp(m2)/n)*(-n*lambertw(b*d*exp(m2)/n)-i2*d+a*d+b*d)/(f*(1+lambertw(b*d*exp(m2)/n))), -i2, -(n*lambertw(b*d*exp(m2)/n)+i2*d-a*d-b*d)/((1+lambertw(b*d*exp(m2)/n))*d);
d/lambertw(b*d*exp(m3)/n), -(n*lambertw(b*d*exp(m3)/n)-b*d)/(b*(1+lambertw(b*d*exp(m3)/n))), lambertw(b*d*exp(m3)/n)*(-n*lambertw(b*d*exp(m3)/n)-i3*d+a*d+b*d)/(f*(1+lambertw(b*d*exp(m3)/n))), -i3, -(n*lambertw(b*d*exp(m3)/n)+i3*d-a*d-b*d)/((1+lambertw(b*d*exp(m3)/n))*d);
d/lambertw(b*d*exp(m4)/n), -(n*lambertw(b*d*exp(m4)/n)-b*d)/(b*(1+lambertw(b*d*exp(m4)/n))), lambertw(b*d*exp(m4)/n)*(-n*lambertw(b*d*exp(m4)/n)-i4*d+a*d+b*d)/(f*(1+lambertw(b*d*exp(m4)/n))), -i4, -(n*lambertw(b*d*exp(m4)/n)+i4*d-a*d-b*d)/((1+lambertw(b*d*exp(m4)/n))*d);
d/lambertw(b*d*exp(m5)/n), -(n*lambertw(b*d*exp(m5)/n)-b*d)/(b*(1+lambertw(b*d*exp(m5)/n))), lambertw(b*d*exp(m5)/n)*(-n*lambertw(b*d*exp(m5)/n)-i5*d+a*d+b*d)/(f*(1+lambertw(b*d*exp(m5)/n))), -i5, -(n*lambertw(b*d*exp(m5)/n)+i5*d-a*d-b*d)/((1+lambertw(b*d*exp(m5)/n))*d)];
x = [1.2; 0.001; 1.5; 1; 15];
for i=1:N
f = double(subs(fs, xs, x));
j = double(subs(jac, xs, x));
x = x - inv(j)*f;
err = abs(inv(j)*f);
if(err<tErr)
break;
end
end
display(x);
im trying to use mesh2d function according to a guide I read.
for some reason im getting all the time this issue:
Undefined function 'mesh2d' for input arguments of type 'double'.
Error in Try1 (line 88)
[p,t] = mesh2d(allnodes, alledges);
I install mesh2d , according to the guide here:
https://github.com/dengwirda/mesh2d
but for some reason im still getting this issue...
this is my code:(im adding the code so it whould be easier in case im missing something, instead il mark the bad part)
clf
file = 'pattern3';
P = imread('Pattern3.png');
P = P(400:3400, 400:3400);
P = 255 - P*6;
P = 1-im2bw(P);
Nmin = min(size(P));
P = P(1:Nmin, 1:Nmin);
[xg, yg] = meshgrid(1:Nmin, 1:Nmin);
P((xg - Nmin/2).^2 + (yg - Nmin/2).^2 > 0.99*0.25*Nmin^2) = 0;
P = padarray(P, [1 1], 0);
CC = bwconncomp(P);
dtheta = pi/24;
theta = (-pi:dtheta:(pi-dtheta))';
nodeouter = [1.1*cos(theta) 1.1*sin(theta)];
Nnodes = length(nodeouter);
nodelist = (1:Nnodes)';
allnodes = nodeouter;
alledges = [nodelist , mod(nodelist, Nnodes)+1];
for n = 1:CC.NumObjects
%for n = 2:2
newP = zeros(size(P));
newP(CC.PixelIdxList{1,n}(:)) = 1;
newP = filter2(fspecial('average',5),newP);
C = contourc(newP,[0.2 0.2]);
C = C(:,2:end)';
C2 = dpsimplify(C,1);
m = 1;
while m <= length(C2(:,1))
if(C2(m,1) == 1 || C2(m,2) == 1)
C2(m,:) = [];
else
m = m + 1;
end
end
C2 = (C2 - Nmin/2)/(Nmin/2);
C = (C - Nmin/2)/(Nmin/2);
figure(1)
hold all
plot(C2(:,1), C2(:,2))
axis image xy
drawnow
nodeinner = C2;
Nnodeshole = length(nodeinner);
nodelist = (1:Nnodeshole)';
edgelist = [nodelist , mod(nodelist, Nnodeshole)+1];
edgelist = edgelist + Nnodes;
allnodes = [allnodes; nodeinner];
alledges = [alledges; edgelist];
Nnodes = Nnodes + Nnodeshole;
n
end
%%
hdata.fun = #(x,y) 0.05*(1 + ((x.^2 + y.^2)/a^2)).^2;
[p,t] = mesh2d(allnodes, alledges); %%here is the issue!!!!!!!!!!!!!!!!!!!!!!!1
%%
as = 0.5;
for n = 1:length(as)
a = as(n);
h = 0;
x = p(:,1);
y = p(:,2);
z = zeros(size(x));
r = sqrt(x.^2 + y.^2);
phi = atan2(y,x);
theta = atan(r/(a+h));
alpha = 2*theta;
xnew = a*sin(alpha).*cos(phi);
ynew = a*sin(alpha).*sin(phi);
znew = -a*cos(alpha);
p2 = [xnew, ynew, znew];
stlwrite('Test.stl', t, p2)
fv.faces = t;
fv.vertices = p2;
clf
figure(3)
patch(fv, 'FaceColor', [1 1 1], 'EdgeColor', 'black', 'LineWidth', 0.1)
axis equal
axis off
xlim([-a a])
ylim([-a a])
zlim([-a a])
camlight head
view(58,28)
zoom(1.5)
drawnow
end
the photo im trying to use:
I recently completely rewrote MESH2D -- bringing it up-to-date with more recent meshing techniques and MATLAB functionality. It looks like you are trying to use subroutines from old versions of the library.
Based on the updates, the routines you want are refine2 and smooth2 (they build and then optimise a two-dimensional constrained Delaunay triangulation).
I recommend that you have a look at the example code contained in tridemo to see how the updated MESH2D toolbox works.
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.
So I was tasked with plotting the Golden Rectangles along with the Golden Spiral over top of the rectangles. We were given the code for the rectangles, for the "spiral" I used multiple arcs from this forum post, and I can only use if and for statements. The code for the rectangles works and the code for the spiral almost works, but what I really need is to get the patch to display behind the plot. This is the code that I have now:
clear variables; close all; clc
phi = (1+sqrt(5))/2;
x0 = 0;
y0 = 0;
for i = 1:8
edgeLength = 1/(phi^(i-1));
moveLength = 1/(phi^i);
modStep = mod(i,4);
if (modStep == 1) %move W, orient NW
x0 = x0 - moveLength;
sx = [x0 x0 x0-edgeLength x0-edgeLength];
sy = [y0 y0+edgeLength y0+edgeLength y0];
%spiral
P1 = [(x0-edgeLength);y0];
P2 = [x0;(y0+edgeLength)];
P0 = [x0;y0];
n = 1000;
v1 = P1-P0;
v2 = P2-P0;
c = det([v1,v2]);
a = linspace(0,atan2(abs(c),dot(v1,v2)),n);
v3 = [0,-c;c,0]*v1;
v = v1*cos(a)+((norm(v1)/norm(v3))*v3)*sin(a);
if i == 1
vx1 = v(1,:);
vy1 = v(2,:);
elseif i == 5
vx6 = (v(1,:)+(1/(phi^5)));
vy6 = (v(2,:)+(1/(phi^5)));
end
end
if (modStep == 2) %move N, orient NE
y0 = y0 + moveLength;
sx = [x0 x0+edgeLength x0+edgeLength x0];
sy = [y0 y0 y0+edgeLength y0+edgeLength];
%spiral
P1 = [x0;(y0+edgeLength)];
P2 = [(x0+edgeLength);y0];
P0 = [x0;y0];
n = 1000;
v1 = P1-P0;
v2 = P2-P0;
c = det([v1,v2]);
a = linspace(0,atan2(abs(c),dot(v1,v2)),n);
v3 = [0,-c;c,0]*v1;
v = v1*cos(a)+((norm(v1)/norm(v3))*v3)*sin(a);
if i == 2
vx2 = v(1,:);
vy2 = (v(2,:)+(1/(phi^2)));
elseif i == 6
vx5 = (v(1,:)+(1/(phi^5)));
vy5 = (v(2,:)+(1/(phi^6)));
end
end
if (modStep == 3) %move E, orient SE
x0 = x0 + moveLength;
sx = [x0 x0 x0+edgeLength x0+edgeLength];
sy = [y0 y0-edgeLength y0-edgeLength y0];
%spiral
P1 = [(x0+edgeLength);y0];
P2 = [x0;(y0-edgeLength)];
P0 = [x0;y0];
n = 1000;
v1 = P1-P0;
v2 = P2-P0;
c = det([v1,v2]);
a = linspace(0,atan2(abs(c),dot(v1,v2)),n);
v3 = [0,-c;c,0]*v1;
v = v1*cos(a)+((norm(v1)/norm(v3))*v3)*sin(a);
if i == 3
vx3 = (v(1,:)+(1/(phi^3)));
vy3 = (v(2,:)+(1/(phi^2)));
elseif i == 7
vx7 = (v(1,:)+(1/(phi^7)));
vy7 = (v(2,:)+(1/(phi^6)));
end
end
if (modStep == 0) %move S, orient SW
y0 = y0 - moveLength;
sx = [x0 x0-edgeLength x0-edgeLength x0];
sy = [y0 y0 y0-edgeLength y0-edgeLength];
%spiral
P1 = [(x0-edgeLength);y0];
P2 = [x0;(y0-edgeLength)];
P0 = [x0;y0];
n = 1000;
v1 = P1-P0;
v2 = P2-P0;
c = det([v1,v2]);
a = linspace(0,atan2(abs(c),dot(v1,v2)),n);
v3 = [0,-c;c,0]*v1;
v = v1*cos(a)+((norm(v1)/norm(v3))*v3)*sin(a);
if i == 4
vx4 = (v(1,:)+(1/(phi^3)));
vy4 = (v(2,:)+(1/(phi^3)));
elseif i == 8
vx8 = (v(1,:)+(1/(phi^7)));
vy8 = (v(2,:)+(1/(phi^7)));
end
end
end
patch(sx,sy,0.8+0.2*rand(1,3));
vx = [vx1 vx2 vx3 vx4 vx5 vx6 vx7 vx8];
vy = [vy1 vy2 vy3 vy4 vy5 vy6 vy7 vy8];
plot(vx,vy);
axis equal
In the code, the comments about North, West, etc. are in reference to the rectangles that are displayed and their orientation. The parts of the code that make up the spiral are likewise labeled.
So I really have two questions, however, the one that is most pressing is why will only the plot(vx,vy) command only work and not the patch? I tried looking at other examples of things but they seem to just type each command in in the order they want them to print and it does it.
The other issue I have it that when I do run this code the arcs work perfectly for i = 1:4 but after that, you can see, the lines are all messed up. I don't really know why that is either.
If I left out any other information I'm sorry!
Thank you
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.