How to make the for loop the length as the array inside the for loop?
10 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I am making a for loop and I want to subtract each element by each other. I was able to do this but my array is now 1x200 and my original array was 1x201. How do I make it so the for loop array is 1x201?
x1 = [1e6:20000:5e6];
L = 4e6; % in m
D_c = 5000;% in m
D1 = [0:25:5000];
x_o = 1e6; % in m
x_2 = [x_o:20000:5e6];
Tw_o = 300; % in K
LR_w = 0.004; % in K/m
LR_c = -0.007; % in K/m
pz_0 = 1000; % in mb
D_1 = D_c.*(sin((pi/L).*(x1-x_o)));
T_c2 = Tw_o-(((LR_w-LR_c).*D_1)-((LR_c.*D1)));
g = 9.81;
Rd = 287;
p1 = pz_0*(((((T_c2)-(LR_c.*D1))./(T_c2))).^((g)/(Rd*LR_c)));
for i = 1:length(p1)-1
deltap(i) = p1(i+1)-p1(i);
end
0 comentarios
Respuestas (1)
Star Strider
el 10 de Oct. de 2023
x1 = [1e6:20000:5e6];
L = 4e6; % in m
D_c = 5000;% in m
D1 = [0:25:5000];
x_o = 1e6; % in m
x_2 = [x_o:20000:5e6];
Tw_o = 300; % in K
LR_w = 0.004; % in K/m
LR_c = -0.007; % in K/m
pz_0 = 1000; % in mb
D_1 = D_c.*(sin((pi/L).*(x1-x_o)));
T_c2 = Tw_o-(((LR_w-LR_c).*D_1)-((LR_c.*D1)));
g = 9.81;
Rd = 287;
p1 = pz_0*(((((T_c2)-(LR_c.*D1))./(T_c2))).^((g)/(Rd*LR_c)));
deltap = diff(p1)
for i = 1:length(p1)-1
deltap(i) = p1(i+1)-p1(i);
end
deltap
If you want to calculate the numerical derivative at eash point instead, use the gradient function —
deltap = gradient(p1)
.
2 comentarios
Star Strider
el 10 de Oct. de 2023
One way would be to take the absolute value (the abs function), the other, since they are uniformly negative, would be to multiply them by -1 or just use a unary negative:
deltap = -gradient(p1)
Use whatever works best in your application.
.
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!