MATLAB simulation runs to infinity, but not when run step by step in debugger - matlab

My code simulates the shock loading of a rocket upon parachute deployment. When the script is run as a whole the value of the shock goes to infinity. However, when the script is run step by step in the debugger mode it works as intended. I am unsure of what is happening or how to fix it. My code is written as follows:
clear all; close all; clc; tic
h_init = 100; %initial altitude, ft
%INPUTS
E = .38e6; %elastic modulus of nylon, psi
A = (1/16)*(1); %cross sectional area of cord, in^2
CD = 2.20; %parachute coefficient of drag
D_p = 144; %parachute diameter, in
W_p = 2; %weight of parachute, lbm
W_r = 105; %dry weight of rocket, lbm
L = 17*3*12; %length of shock cord, in
v_init = -350; %initial upwards velocity of rocket/parachute, ft/s
dt = 0.00001; %simulation time step, s
t_sim = 4; %total simulation time, s
g = 32.2; %gravitational acceleration, ft/s^2
%CALCULATIONS
k = (3900)./L; %spring constant, lbf/in
S_p = pi*D_p.^2/4/144; %parachute cross sectional area, ft^2
m_p = W_p/g; %mass of parachute, slugs
m_r = W_r./g; %mass of rocket, slugs
N = ceil(t_sim/dt);
N_L = length(W_r);
time = dt:dt:t_sim + dt;
Y1 = zeros(N_L,N);
Y2 = zeros(N_L,N);
V1 = zeros(N_L,N);
V2 = zeros(N_L,N);
FS = zeros(N_L,N);
Dr = zeros(N_L,N);
Shock = zeros(N_L,1);
Exten = zeros(N_L,1);
VDiff = zeros(N_L,1);
DrMax = zeros(N_L,1);
TDrop = zeros(N_L,1);
for j = 1:N_L
v1 = v_init; %initial velocity of parachute after deployment, ft/s
y1 = 0; %initial parachute position relative to deployment altitude, ft
v2 = v_init; %initial velocity of rocket after deployment, ft/s
y2 = 0; %initial position of rocket relative to deployment altitude, ft
Y1(j,1) = y1;
Y2(j,1) = y2;
V1(j,1) = v1;
V2(j,1) = v2;
FS(j,1) = 0;
Dr(j,1) = 0;
slack = 1;
for i = 1:N
rho = findDensity(h_init+y1);%air density, slug/ft^3
Q = 144*0.5*rho*v1^2; %dynamic pressure, psi
D = -sign(v1)*(Q/144)*S_p*CD; %drag, lbf
if y1-y2 >= L/12
delta = 12*(y1-y2)-L; %shock cord extension, in
if slack ==1
TDrop(j) = dt*i;
slack = 0;
end
elseif y2-y1 >= L/12
delta = -12*(y1-y2)-L; %shock cord extension, in
else
delta = 0;
end
fs = k*delta; %spring force, lbf
F1 = D - W_p - fs; %net force on parachute, lbf
F2 = fs - W_r(j); %net force on rocket, lbf
a1 = F1/m_p; %acceleration of parachute, ft/s^2
a2 = F2/m_r(j); %acceleration of rocket, ft/s^2
y1 = y1 + v1*dt; %update parachute position, ft
y2 = y2 + v2*dt; %update rocket position, ft
v1 = v1 + a1*dt; %update parachute velocity, ft/s
v2 = v2 + a2*dt; %update rocket velocity, ft/s
Y1(j,i+1) = y1;
Y2(j,i+1) = y2;
V1(j,i+1) = v1;
V2(j,i+1) = v2;
FS(j,i+1) = fs;
Dr(j,i+1) = D;
end
Shock(j) = max(FS(j,:));
Exten(j) = 12*max(Y1(j,:)-Y2(j,:)-L/12);
VDiff(j) = max(abs(V1(j,:)-V2(j,:)));
DrMax(j) = max(Dr(j,:));
end
H1 = h_init+Y1;
H2 = h_init+Y2;
fprintf('Max Shock Load: %.2f lbf', Shock)
toc
function density = findDensity(h)
temp = findTemp(h);
pressure = findPressure(h);
density = pressure / (1718 * (temp + 459.7));
end
function pressure = findPressure(h)
temp = findTemp(h);
if(h > 82345)
pressure = 51.97 * (((temp + 459.7) / 389.98) ^ -11.388);
elseif(h > 36152)
pressure = 473.1 * exp(1.73 - (0.000048 * h));
else
pressure = 2116 * (((temp + 459.7) / 518.6) ^ 5.256);
end
end
function temp = findTemp(h)
if(h > 82345)
temp = (0.00164 * h) - 205.5;
elseif(h > 36152)
temp = -70;
else
temp = 59 - (0.00356 * h);
end
end

Related

Servo motor routing according to Matlab x, y, z coordinates

I work to about missile routing. I calculated throuhout the flight x,y,z coordinates of missile . I have data set about missile x,y,z coordinates. My goal is to move the servo motor according to x, y, z coordinates.
My input is 3-dimensional(x,y,z). I want to simulate in two dimensions. And for this I use vectoral calculation. The servo motor can take values between 0-1. But the result larger than 1 . When the results are reduced at the same rate, result is smaller than 0. But I get still the error
Undefined function 'writePosition' for input arguments of type 'matlab.graphics.chart.primitive.Surface'.
I will be grateful if you could help me.
My data example:
missile_x = 0.015
missile_y = 0.054
missile_z = 0.254
missile_flight = 0.00018794
My flight rotation code:
missile_x = vpa(Xval{id}(k)/10,5)
missile_y = vpa(Yval{id}(k)/10,5)
missile_z = vpa(Zval{id}(k)/10,5)
missile_flight = vpa(0.00555556*(missile_x^2+missile_y^2+missile_z^2)^1/2,5)
writePosition(s, missile_flight);
current_pos = readPosition(s);
current_pos = current_pos*missile_flight;
fprintf('Current motor position is %d degrees\n', current_pos);
pause(2);
Missile X,Y,Z calculation code:
dt = 0.005; %time step
g = 9.81; %gravity
ro = 1.2; %air density
A = pi*(0.2)^2; % reference area
Vmag = 0; % missile velocity vectoral value [m/sn]
t = 0;
T(1) = t;
U(1) = 0; %the missile is initially at rest at t = 0; So the velocity is 0
V(1) = 0;
W(1) = 0;
X(1) = X0;
Y(1) = Y0;
Z(1) = Z0;
n = 1;
h = interp2(x_terrain, y_terrain, h_terrain,X(1), Y(1));
while (Z(n) >= h)
[Thx, Thy, Thz] = thrust(t, Thmag0, theta, phi, Tburn, U(n), V(n), W(n));
Vmag = (U(n)^2 + V(n)^2 + W(n)^2)^(1/2);
Thmag = (Thx^2 + Thy^2 + Thz^2)^(1/2);
m = mass(t, m0, mf, Tburn);
[rho,sound_speed] = atmosphere(Z(n));
Cd = drag_coeff(Vmag,sound_speed);
U(n+1) = U(n) + (Thx/m - (Cd*rho*A/(2*m))*(U(n)*(U(n)^2+V(n)^2+W(n)^2)^(1/2)))*dt;
V(n+1) = V(n) + (Thy/m - (Cd*rho*A/(2*m)*(V(n)*(U(n)^2+V(n)^2+W(n)^2)^(1/2))))*dt;
W(n+1) = W(n) + (Thz/m - (Cd*rho*A/(2*m))*(W(n)*(U(n)^2+V(n)^2+W(n)^2)^(1/2)) - g)*dt;
X(n+1) = X(n) + U(n+1)*dt;
Y(n+1) = Y(n) + V(n+1)*dt;
Z(n+1) = Z(n) + W(n+1)*dt;
h = interp2(x_terrain, y_terrain, h_terrain, ...
X(end), Y(end));
t = t + dt;
T(n+1) = t;
n = n+1 ;
end
Tval = T;
Xval = X;
Yval = Y;
Zval = Z;

Matlab neural network for regression

I have implemented 3 function for neural network regression:
1) a forward propagation function that given the training inputs and the net structure calculates the predicted output
function [y_predicted] = forwardProp(Theta,Baias,Inputs,NumberOfLayers,RegressionSwitch)
for i = 1:size(Inputs{1},2)
Activation = (Inputs{1}(:,i))';
for j = 2:NumberOfLayers - RegressionSwitch
Activation = 1./(1+exp(-(Activation*Theta{j-1} + Baias{j-1})));
end
if RegressionSwitch == 1
y_predicted(:,i) = Activation*Theta{end} + Baias{end};
else
y_predicted(:,i) = Activation;
end
end
end
2) a cost function that given the predicted and the desired output, calculates the cost of the network
function [Cost] = costFunction(y_predicted, y, Theta, Baias, Lambda)
Cost = 0;
for j = 1:size(y,2)
for i = 1:size(y,1)
Cost = Cost +(((y(i,j) - y_predicted(i,j))^2)/size(y,2));
end
end
Reg = 0;
for i = 1:size(Theta, 2)
for j = 1:size(Theta{i}, 1)
for k = 1:size(Theta{i}, 2)
Reg = Reg + (Theta{i}(j,k))^2;
end
end
end
for i = 1:size(Baias, 2)
for j = 1:length(Baias{i})
Reg = Reg + (Baias{i}(j))^2;
end
end
Cost = Cost + (Lambda/(2*size(y,2)))*Reg;
end
3) a back propagation function that calculates the partial derivative of the cost function for each weight in the network
function [dTheta, dBaias] = Deltas(Theta,Baias,Inputs,NumberOfLayers,RegressionSwitch, Epsilon, Lambda, y)
for i = 1:size(Theta,2)
for j = 1:size(Theta{i},1)
for k = 1:size(Theta{i},2)
dTp = Theta;
dTm = Theta;
dTp{i}(j,k) = dTp{i}(j,k) + Epsilon;
dTm{i}(j,k) = dTm{i}(j,k) - Epsilon;
y_predicted_p = forwardProp(dTp,Baias,Inputs,NumberOfLayers,RegressionSwitch);
y_predicted_m = forwardProp(dTm,Baias,Inputs,NumberOfLayers,RegressionSwitch);
Cost_p = costFunction(y_predicted_p, y, dTp, Baias, Lambda);
Cost_m = costFunction(y_predicted_m, y, dTm, Baias, Lambda);
dTheta{i}(j,k) = (Cost_p - Cost_m)/(2*Epsilon);
end
end
end
for i = 1:size(Baias,2)
for j = 1:length(Baias{i})
dBp = Baias;
dBm = Baias;
dBp{i}(j) = dTp{i}(j) + Epsilon;
dBm{i}(j) = dTm{i}(j) - Epsilon;
y_predicted_p = forwardProp(Theta,dBp,Inputs,NumberOfLayers,RegressionSwitch);
y_predicted_m =forwardProp(Theta,dBm,Inputs,NumberOfLayers,RegressionSwitch);
Cost_p = costFunction(y_predicted_p, y, Theta, dBp, Lambda);
Cost_m = costFunction(y_predicted_m, y, Theta, dBm, Lambda);
dBaias{i}(j) = (Cost_p - Cost_m)/(2*Epsilon);
end end end
I train the neural network with data from an exact mathematical function of the inputs.
The gradient descent seems to work as the cost decrease each iteration, but when i test the trained network the regression is terrible.
The functions are not meant to be efficient, but they should work so I am really frustrated to see they don't... The main function and the data are ok so the problem should be here. Can you please help me to spot it?
here is the "main":
clear;
clc;
Nodes_X = 5;
Training_Data = 1000;
x = rand(Nodes_X, Training_Data)*3;
y = zeros(2,Training_Data);
for j = 1:Nodes_X
for i = 1:Training_Data
y(1,i) = (x(1,i)^2)+x(2,i)-x(3,i)+2*x(4,i)/x(5,i);
y(2,i) = (x(5,i)^2)+x(2,i)-x(3,i)+2*x(4,i)/x(1,i);
end
end
vx = rand(Nodes_X, Training_Data)*3;
vy = zeros(2,Training_Data);
for j = 1:Nodes_X
for i = 1:Training_Data
vy(1,i) = (vx(1,i)^2)+vx(2,i)-vx(3,i)+2*vx(4,i)/vx(5,i);
vy(2,i) = (vx(5,i)^2)+vx(2,i)-vx(3,i)+2*vx(4,i)/vx(1,i);
end
end
%%%%%%%%%%%%%%%%%%%%%%ASSIGN NODES TO EACH LAYER%%%%%%%%%%%%%%%%%%%%%%%%%%%
NumberOfLayers = 4;
Nodes(1) = 5;
Nodes(2) = 10;
Nodes(3) = 10;
Nodes(4) = 2;
if length(Nodes) ~= NumberOfLayers || (Nodes(1)) ~= size(x, 1)
WARNING = msgbox('Nodes assigned incorrectly!');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%INITIALIZATION%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i = 1:NumberOfLayers-1
Theta{i} = rand(Nodes(i),Nodes(i+1));
Baias{i} = rand(1,Nodes(i+1));
end
Inputs{1} = x;
Outputs{1} = y;
RegressionSwitch = 1;
Lambda = 10;
Epsilon = 0.00001;
Alpha = 0.01;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%TRAINING%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Epoch = 0;
figure;
hold on;
while Epoch <=20
%%%%%%%%%%%%%%%%%%%%FORWARD PROPAGATION%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
y_predicted = forwardProp(Theta,Baias,Inputs,NumberOfLayers,RegressionSwitch);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%COST%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Cost = costFunction(y_predicted, y, Theta, Baias, Lambda);
scatter(Epoch,Cost);
pause(0.01);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%BACK PROPAGATION%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[dTheta, dBaias] = Deltas(Theta,Baias,Inputs,NumberOfLayers,RegressionSwitch, Epsilon, Lambda, y);
%%%%%%%%%%%%%%%GRADIENT DESCENT%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i = 1:size(Theta,2)
Theta{i} = Theta{i}-Alpha*dTheta{i};
end
for i = 1:size(Baias,2)
Baias{i} = Baias{i}-Alpha*dBaias{i};
end
Epoch = Epoch + 1;
end
hold off;
V_Inputs{1} = vx;
V_y_predicted = forwardProp(Theta,Baias,V_Inputs,NumberOfLayers,RegressionSwitch);
figure;
hold on;
for i = 1:size(vy,2)
scatter(vy(1,i),V_y_predicted(1,i));
pause(0.01);
end
hold off;
figure;
hold on;
for i = 1:size(vy,2)
scatter(vy(2,i),V_y_predicted(2,i));
pause(0.01);
end
hold off;

Why do the plot and patch commands both not execute in Matlab?

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

Undefined variable in 'if' statement

I'm writing a script for an aerodynamics class and I'm getting the following error:
Undefined function or variable 'dCt_dx'.
Error in Project2_Iteration (line 81)
Ct = trapz(x,dCt_dx)
I'm not sure what the cause is. It's something to do with my if statement. My script is below:
clear all
clc
global dr a n Vinf Vr w rho k x c cl dr B R beta t
%Environmental Parameters
n = 2400; %rpm
Vinf = 154; %KTAS
rho = 0.07647 * (.7429/.9450); %from mattingly for 8kft
a = 1084; %speed of sound, ft/s, 8000 ft
n = n/60; %convert to rps
w = 2*pi*n;
Vinf = (Vinf*6076.12)/3600; %convert from KTAS to ft/s
k = length(c);
dr = R/k; %length of each blade element
for i = 1:k
r(i) = i*dr - (.5*dr); %radius at center of blade element
dA = 2*pi*r*dr; %Planform area of blade element
x(i) = r(i)/R;
if x(i) > .15 && x(i-1) < .15
i_15 = i;
end
if x(i) > .75 && x(i-1) < .75
i_75h = i;
i_75l = i-1;
end
Vr(i) = w*r(i) + Vinf;
%Aerodynamic Parameters
M = Vr(i)/a;
if M > 0.9
M = 0.9;
end
m0 = 0.9*(2*pi/(1-M^2)^0.5); %lift-curve slope (2pi/rad)
%1: Calculate phi
phi = atan(Vinf/(2*pi*n*r(i)));
%2: Choose Vo
Vo = .00175*Vinf;
%3: Calculate Theta
theta = atan((Vinf + Vo)/(2*pi*n*r(i)))-phi;
%4:
if option == 1
%calculate cl(i) from c(i)
sigma = (B*c(i))/(pi*R);
if sigma > 0
cl(i) = (8*x(i)*theta*cos(phi)*tan(phi+theta))/sigma;
else
cl(i) = 0;
end
else %option == 2
%calculate c(i) from cl(i)
if cl(i) ~= 0
sigma = (8*x(i)*theta*cos(phi)*tan(phi+theta))/cl(i);
else
sigma = 0;
end
c(i) = (sigma*pi*R)/B;
if c(i) < 0
c(i) = 0;
end
end
%5: Calculate cd
cd(i) = 0.0090 + 0.0055*(cl(i)-0.1)^2;
%6: calculate alpha
alpha = cl(i)/m0;
%7: calculate beta
beta(i) = phi + alpha + theta;
%8: calculate dCt/dx and dCq/dx
phi0 = phi+theta;
lambda_t = (1/(cos(phi)^2))*(cl(i)*cos(phi0) - cd(i)*sin(phi0));
lambda_q = (1/(cos(phi)^2))*(cl(i)*sin(phi0) + cd(i)*cos(phi0));
if x(i) >= 0.15
dCt_dx(i) = ((pi^3)*(x(i)^2)*sigma*lambda_t)/8; %Roskam eq. 7.47, pg. 280
dCq_dx(i) = ((pi^3)*(x(i)^3)*sigma*lambda_q)/16; %Roskam eq. 7.48, pg 280
else
dCt_dx(i) = 0;
dCq_dx(i) = 0;
end
%calculate Mdd
t(i) = (0.04/(x(i)^1.2))*c(i);
Mdd(i) = 0.94 - (t(i)/c(i)) - cl(i)/10;
end
%9: calculate Ct, Cq, Cd
Ct = trapz(x,dCt_dx)
Cq = trapz(x,dCq_dx)
D = 2*R;
Q=(rho*(n^2)*(D^5)*Cq)
T=(rho*(n^2)*(D^4)*Ct)
When I step through your script, I see that the the entire for i = 1:k loop is skipped because k=0. You set k = length(c), but c was never initialized to a value, so it has length zero.
Because of this, dCt_dx is never given a value--and more importantly the majority of your script is never run.
If you're going to be using MATLAB in the future, I really suggest learning how to do this. It makes it a lot easier to find bugs. Try looking at this video.

Perform step-by-step integral

I have this piece of code:
time = 614.4;
Uhub = 11;
HubHt = 90;
TI = 'A';
N1 = 4096;
N2 = 32;
N3 = 32;
L1 = Uhub*time;
L2 = 150;
L3 = 220;
V = L1*L2*L3;
gamma = 3.9;
c = 1.476;
b = 5.6;
if HubHt < 60
lambda1 = 0.7*HubHt;
else
lambda1 = 42;
end
L = 0.8*lambda1;
if isequal(TI,'A')
Iref = 0.16;
sigma1 = Iref*(0.75*Uhub + b);
elseif isequal(TI,'B')
Iref = 0.14;
sigma1 = Iref*(0.75*Uhub + b);
elseif isequal(TI,'C')
Iref = 0.12;
sigma1 = Iref*(0.75*Uhub + b);
else
sigma1 = str2num(TI)*Uhub/100;
end
sigma_iso = 0.55*sigma1;
%% Wave number vectors
ik1 = cat(2,(-N1/2:-1/2),(1/2:N1/2));
ik2 = -N2/2:N2/2-1;
ik3 = -N3/2:N3/2-1;
[x y z] = ndgrid(ik1,ik2,ik3);
k1 = reshape((2*pi*L/L1)*x,N1*N2*N3,1);
k2 = reshape((2*pi*L/L2)*y,N1*N2*N3,1);
k3 = reshape((2*pi*L/L3)*z,N1*N2*N3,1);
k = sqrt(k1.^2 + k2.^2 + k3.^2);
Now I should calculate
where
The procedure to calculate the integral is
At the moment I'm using this loop
E = #(k) (1.453*k.^4)./((1 + k.^2).^(17/6));
E_int = zeros(1,N1*N2*N3);
E_int(1) = 1.5;
for i = 2:(N1*N2*N3)
E_int(i) = E_int(i) + quad(E,i-1,i);
end
neglecting for the k>400 approximation. I believe that my loop is not right.
How would you suggest to calculate the integral?
I thank you in advance.
WKR,
Francesco
This is a list of correction from the more obvious to the possibly more subtle. (Indeed I start from what you wrote in the final part going upwards).
From what you write:
E = #(k) (1.453*k.^4)./((1 + k.^2).^(17/6));
E_int = zeros(1,N1*N2*N3);
E_int(1) = 1.5;
for i = 2:(N1*N2*N3)
%//No point in doing this:
%//E_int(i) = E_int(i) + quad(E,i-1,i);
%//According to what you write, it should be:
E_int(i) = E_int(i-1) + quad(E,i-1,i);
end
You could speed the whole thing up by doing
%//Independent integration on segments
Local_int = arrayfun(#(i)quad(E,i-1,i), 2:(N1*N2*N3));
Local_int = [1.5 Local_int];
%//integral additivity
E_int = cumsum(Local_int);
Moreover, if the known condition (point 2.) really is "... ( = 1.5 if k' = 0)", then the whole implementation should really be more like
%//Independent integration on segments
Local_int = arrayfun(#(i)quad(E,i-1,i), 2:(N1*N2*N3));
%//integral additivity + cumulative removal of queues
E_int = 1.5 - [0 fliplr(cumsum(fliplr(Local_int)))]; %//To remove queues