Nonlinear function in Matlab? - matlab

I have forward transformations given as below:
Tx: x' = a1x + a2y + a3xy + a4.
Tx: y' = b1x + b2y + b3xy + b4.
function y = transformationTest (I,a1,a2,a3,a4,b1,b2,b3,b4)
[M N] = size(I);
for i = 1: M
for j = 1: N
x1 = a1*i + a2*j + a3*i*j + a4;
y1 = b1*i + b2*j + b3*i*j + b4;
y(round(x1+1),round(y1+1)) = I(i,j);
end
end
end
Is my implementation wrong or what because I am getting to large images and meaningless.

Related

Trying to solve 2D truss structure, error: Warning: Solution does not exist because the system is inconsistent

Trying to make a systems of equations to solve the internal forces of a truss in the x and y plane, and get the error that my system is inconsistent. I have double checked multiple times and I'm not quite sure what I'm missing, any tips?
Here is code:
clc, clear all;
syms FAB FBC FCD FDE FEF FFG FGH FAI FCI FHI FBI FCG FCF FDF FGI Ay Ex Ey;
theta1 = atand(4/10); %theta for all members
%creating vectors for all internal member directions
VecFAB = FAB*[1 0];
VecFBC = FBC*[1 0];
VecFCD = FCD*[1 0];
VecFDE = FDE*[1 0];
VecFEF= FEF*[cosd(theta1) sind(theta1)];
VecFFG = FFG*[cosd(theta1) sind(theta1)];
VecFGH = FGH*[cosd(theta1) sind(theta1)];
VecFAI = FAI*[cosd(theta1) sind(theta1)];
VecFCI = FCI*[cosd(theta1) sind(theta1)];
VecFCF = FCF*[cosd(theta1) sind(theta1)];
VecFGI = FGI*[cosd(theta1) sind(theta1)];
VecFHI = FHI*[0 1];
VecFBI = FBI*[0 1];
VecFCG = FCG*[0 1];
VecFDF = FDF*[0 1];
RA = Ay*[0 1];
RE = Ey*[0 1] + Ex*[1 0];
F1 = 2000*[1 0];
F2 = 1500*[1 0];
F3 = 3000*[0 -1];
F4 = 3000*[0 -1];
%create equations at given points
FA = VecFAB + VecFAI + RA + 0*(VecFAB+VecFBC+VecFCD+VecFDE+VecFEF+VecFFG+VecFGH+VecFAI+VecFCI+VecFCF+VecFGI+VecFDF+VecFCG+VecFBI+VecFHI+RA+RE);
FB = VecFAB + VecFBI + VecFBC + 0*(VecFAB+VecFBC+VecFCD+VecFDE+VecFEF+VecFFG+VecFGH+VecFAI+VecFCI+VecFCF+VecFGI+VecFDF+VecFCG+VecFBI+VecFHI+RA+RE);
FC = VecFBC + VecFCI + VecFCD + VecFCF + VecFCG + 0*(VecFAB+VecFBC+VecFCD+VecFDE+VecFEF+VecFFG+VecFGH+VecFAI+VecFCI+VecFCF+VecFGI+VecFDF+VecFCG+VecFBI+VecFHI+RA+RE);
FD = VecFDF + VecFCD + VecFDE + 0*(VecFAB+VecFBC+VecFCD+VecFDE+VecFEF+VecFFG+VecFGH+VecFAI+VecFCI+VecFCF+VecFGI+VecFDF+VecFCG+VecFBI+VecFHI+RA+RE);
FE = RE + VecFEF + VecFDE + 0*(VecFAB+VecFBC+VecFCD+VecFDE+VecFEF+VecFFG+VecFGH+VecFAI+VecFCI+VecFCF+VecFGI+VecFDF+VecFCG+VecFBI+VecFHI+RA+RE);
FF = F4 + VecFEF + VecFFG + VecFDF + VecFCF + 0*(VecFAB+VecFBC+VecFCD+VecFDE+VecFEF+VecFFG+VecFGH+VecFAI+VecFCI+VecFCF+VecFGI+VecFDF+VecFCG+VecFBI+VecFHI+RA+RE);
FG = F3 + VecFFG + VecFGH + VecFGI + VecFCG + 0*(VecFAB+VecFBC+VecFCD+VecFDE+VecFEF+VecFFG+VecFGH+VecFAI+VecFCI+VecFCF+VecFGI+VecFDF+VecFCG+VecFBI+VecFHI+RA+RE);
FH = F1 + VecFHI + VecFGH + 0*(VecFAB+VecFBC+VecFCD+VecFDE+VecFEF+VecFFG+VecFGH+VecFAI+VecFCI+VecFCF+VecFGI+VecFDF+VecFCG+VecFBI+VecFHI+RA+RE);
FI = F2 + VecFBI + VecFHI + VecFAI + VecFCI + VecFGI + 0*(VecFAB+VecFBC+VecFCD+VecFDE+VecFEF+VecFFG+VecFGH+VecFAI+VecFCI+VecFCF+VecFGI+VecFDF+VecFCG+VecFBI+VecFHI+RA+RE);
%break into x and y components
EQNFAX = FA(1)==0;
EQNFAY = FA(2)==0;
EQNFBX = FB(1)==0;
EQNFBY = FB(2)==0;
EQNFCX = FC(1)==0;
EQNFCY = FC(2)==0;
EQNFDX = FD(1)==0;
EQNFDY = FD(2)==0;
EQNFEX = FE(1)==0;
EQNFEY = FE(2)==0;
EQNFFX = FF(1)==0;
EQNFFY = FF(2)==0;
EQNFGX = FG(1)==0;
EQNFGY = FG(2)==0;
EQNFHX = FH(1)==0;
EQNFHY = FH(2)==0;
EQNFIX = FI(1)==0;
EQNFIY = FI(2)==0;
[A,B] = equationsToMatrix([EQNFAX,EQNFAY,EQNFBX,EQNFBY,EQNFCX,EQNFCY,EQNFDX,EQNFDY,EQNFEX,EQNFEY,EQNFFX,EQNFFY,EQNFGX,EQNFGY,EQNFHX,EQNFHY,EQNFIX,EQNFIY],[FAB,FBC,FCD,FDE,FEF,FFG,FGH,FAI,FCI,FHI,FBI,FCG,FCF,FDF,FGI,Ay,Ex,Ey]);
solution = linsolve(A,B)
I have tried double checking all my equations, as well as the vectors that represent them, and still no dice. I have done this before on a problem to solve force and reaction forces in 3 dimensions and had less trouble, the code is below if it might help.
clear all, clc
syms Ax Ay Bx Bz Cz F2 %Cy is not present because the problem states that Cy = 0
F1 = 300; %given F1 value
rF2 = [cos(45*pi/180)*sin(30*pi/180) cos(45*pi/180)*cos(30*pi/180) -sin(45*pi/180)]; %using given angles, break F1 and F2 down into x, y, and z direction
rF1 = [0 -cos(45*pi/180) -sin(45*pi/180)];
uF1 = rF1/norm(rF1); %create unit vectors for F1 and F2 so that they can be broken down into x, y, and z components
uF2 = rF2/norm(rF2);
vecF1 = F1*uF1; %create the vector forms of F1 and F2 by multiplying the unit vector by F1 and F2 respectively
vecF2 = F2*uF2;
vecAx = Ax*[1 0 0]; %multiply Ax, Ay, Bx, Bz, and Cz by their respective unit in matrix form, to create correct syntax for code
vecAy = Ay*[0 1 0];
vecBx = Bx*[1 0 0];
vecBz = Bz*[0 0 1];
vecCz = Cz*[0 0 1];
sumF = vecF1 + vecF2 + vecAx + vecAy + vecBx + vecBz + vecCz; %create main sum of forces equation to be broken down into x, y, z components
eqn1=sumF(1)==0; %create equations equivalent to sum of forces in x, y, and z using matrix made by sumF
eqn2=sumF(2)==0;
eqn3=sumF(3)==0;
rF1_D = [0 -5 5]; %find distance from created point 'D' (where F2 lies) to use for moment calculation
rF2_D = [0 0 0];
rA_D = [0 -5 4];
rB_D = [0 -3 0];
rC_D = [-5 0 0];
sumM = (cross(vecF1, rF1_D) + cross(vecF2, rF2_D)) ...
+ (cross(vecAx, rA_D) + cross(vecAy, rA_D) + cross(vecBx, rB_D) + cross(vecBz, rB_D) ...
+ cross(vecCz, rC_D)); %create the sum of moments main equation in matrix form to be broken down into x, y, and z components using the cross product of each vector with the distance from point D
eqn4=sumM(1)==0; %create equations equivalent to the sum of moments at x, y, and z
eqn5=sumM(2)==0;
eqn6=sumM(3)==0;
[A,B] = equationsToMatrix([eqn1 eqn2 eqn3 eqn4 eqn5 eqn6], [Ax Ay Bx Bz Cz F2]); %creating a matrix based on the sum of forces and sum of moment equations, solving for reaction forces and F2
solution = linsolve(A,B); %solving matrix for reaction forces and f2
F2=round(solution(6),3) %printed F2; solution was a very long fraction, simplified to 3 decimals

How to fix Matlab Solve Calculation Error/solver gives a variable back instead of a numerical answer?

I'm trying to solve for q. However, this error, in the image attached, shows up and I have no clue how to fix it. The code runs smoothly without the fifth blob [k5*z5/(1+q*(k5-1))] in the equation and it solves for q. But when I add it to the equation, you get the error... Help....
z1 = 0.01188354;
z2 = 0.20291147;
z3 = 0.03386809;
z4 = 0.6087344;
z5 = 0.1426025;
k1 = 0.00211577;
k2 = 433.816504;
k3 = 0.00651267;
k4 = 12.8652437;
k5 = 3.25E-06;
syms q
eqn = k1*z1/(1+q*(k1-1)) + k2*z2/(1+q*(k2-1)) + k3*z3/(1+q*(k3-1)) + k4*z4/(1+q*(k4-1))+ k5*z5/(1+q*(k5-1)) == 1;
qvalue = solve(eqn,q,'Real',true)
You messed a bit with solve. It looks like your equation doesn't have real solution and you force it with 'Real',true It should be:
z1 = 0.01188354;
z2 = 0.20291147;
z3 = 0.03386809;
z4 = 0.6087344;
z5 = 0.1426025;
k1 = 0.00211577;
k2 = 433.816504;
k3 = 0.00651267;
k4 = 12.8652437;
k5 = 3.25E-06;
syms q
eqn = k1*z1/(1+q*(k1-1)) + k2*z2/(1+q*(k2-1)) + k3*z3/(1+q*(k3-1)) + k4*z4/(1+q*(k4-1))+ k5*z5/(1+q*(k5-1)) == 1;
qvalue = solve(eqn,q)
And the output is:
root(z^5 - (553951988707451897271725042874967932463648393519489496501295701298760653356885564069*z^4)/146344515065711958525050250689493211538989295698771861545387136571113311336350613504 + (877647369043978904163970290106419962713606423192038983322127636310825879207936*z^3)/164487327751721471763565140886077743965784030575734255347857322244873703346919 - (3796080934806054258394941458065140559250786119491250988496078448019381612969984*z^2)/1151411294262050302344955986202544207760488214030139787435001255714115923428433 + (838076006172751803233223399262272123114675605549658858976242221943722830987264*z)/1151411294262050302344955986202544207760488214030139787435001255714115923428433 + 21452770321210561747759866301918995558818778263453224281332504760441104236544/1151411294262050302344955986202544207760488214030139787435001255714115923428433, z, 1)
root(z^5 - (553951988707451897271725042874967932463648393519489496501295701298760653356885564069*z^4)/146344515065711958525050250689493211538989295698771861545387136571113311336350613504 + (877647369043978904163970290106419962713606423192038983322127636310825879207936*z^3)/164487327751721471763565140886077743965784030575734255347857322244873703346919 - (3796080934806054258394941458065140559250786119491250988496078448019381612969984*z^2)/1151411294262050302344955986202544207760488214030139787435001255714115923428433 + (838076006172751803233223399262272123114675605549658858976242221943722830987264*z)/1151411294262050302344955986202544207760488214030139787435001255714115923428433 + 21452770321210561747759866301918995558818778263453224281332504760441104236544/1151411294262050302344955986202544207760488214030139787435001255714115923428433, z, 2)
root(z^5 - (553951988707451897271725042874967932463648393519489496501295701298760653356885564069*z^4)/146344515065711958525050250689493211538989295698771861545387136571113311336350613504 + (877647369043978904163970290106419962713606423192038983322127636310825879207936*z^3)/164487327751721471763565140886077743965784030575734255347857322244873703346919 - (3796080934806054258394941458065140559250786119491250988496078448019381612969984*z^2)/1151411294262050302344955986202544207760488214030139787435001255714115923428433 + (838076006172751803233223399262272123114675605549658858976242221943722830987264*z)/1151411294262050302344955986202544207760488214030139787435001255714115923428433 + 21452770321210561747759866301918995558818778263453224281332504760441104236544/1151411294262050302344955986202544207760488214030139787435001255714115923428433, z, 3)
root(z^5 - (553951988707451897271725042874967932463648393519489496501295701298760653356885564069*z^4)/146344515065711958525050250689493211538989295698771861545387136571113311336350613504 + (877647369043978904163970290106419962713606423192038983322127636310825879207936*z^3)/164487327751721471763565140886077743965784030575734255347857322244873703346919 - (3796080934806054258394941458065140559250786119491250988496078448019381612969984*z^2)/1151411294262050302344955986202544207760488214030139787435001255714115923428433 + (838076006172751803233223399262272123114675605549658858976242221943722830987264*z)/1151411294262050302344955986202544207760488214030139787435001255714115923428433 + 21452770321210561747759866301918995558818778263453224281332504760441104236544/1151411294262050302344955986202544207760488214030139787435001255714115923428433, z, 4)
root(z^5 - (553951988707451897271725042874967932463648393519489496501295701298760653356885564069*z^4)

Confusion in MATLAB filter roll off

I have the following code in matlab:
L = 10000;
Power = -100;
A = 10^0.5;
Fs = 25e6;
fc1 = 100;
fc2 = 1e3;
fc3 = 10e3;
fc4 = 100e3;
fc5 = 1e6;
a1 = 2*pi*fc1/Fs;
a2 = 2*pi*fc2/Fs;
a3 = 2*pi*fc3/Fs;
a4 = 2*pi*fc4/Fs;
a5 = 2*pi*fc5/Fs;
x = wgn(1,L,Power);
y1 = zeros(1,L);
y2 = zeros(1,L);
y3 = zeros(1,L);
y4 = zeros(1,L);
y5 = zeros(1,L);
y = zeros(1,L);
for i = 2:L,
y1(i) = (1-a1)*y1(i-1) + a1*x(i);
y2(i) = (1-a2)*y2(i-1) + a2*x(i)/A;
y3(i) = (1-a2)*y3(i-1) + a3*x(i)/A^2;
y4(i) = (1-a2)*y4(i-1) + a4*x(i)/A^3;
y5(i) = (1-a2)*y5(i-1) + a5*x(i)/A^4;
y(i) = y1(i) + y2(i) + y3(i) + y4(i) + y5(i);
end
fft1 = fft(y);
fft1 = fft1(1:length(y)/2+1);
psd1 = (1/(Fs*length(y)))*abs(fft1).^2;
psd1(2:end-1) = 2*psd1(2:end-1);
freq = 0:Fs/length(y):Fs/2;
figure(3);
semilogx(freq,10*log10(psd1))
grid on
Ts = 40e-9;
z = tf('z',Ts);
H1 = a1/(1-(1-a1)*z^-1);
H2 = (a2/A)/(1-(1-a2)*z^-1);
H3 = (a3/A^2)/(1-(1-a3)*z^-1);
H4 = (a4/A^3)/(1-(1-a4)*z^-1);
H5 = (a5/A^4)/(1-(1-a5)*z^-1);
H = (H1 + H2 + H3 + H4 + H5);
figure(5);
bode(H),grid
The intent of this code is to model flicker noise, which has 10dB/dec of slope in its power spectral density.
To model that there is a filter whose output is y(i) and input x(i) which is white guassian noise in this case. In the bode plot of the filter (labelled as figure 5) I can see that it has 10dB/dec of roll-off as I intended.
But when I check the output noise (y(i) in this case) power spectral density (labelled as figure 3) I am seeing 20dB/dec of roll-off.
Could someone please explain what I did wrong and why I am not able to get the 10dB roll-off in power spectral density?
Your bode plot uses a transfer function which is the sum of 5 similar subfilters, each using a coefficient ai. In your implementation, you should similarly have 5 subfilters with a regular structure. Howerver, while copying each line you have left some of the coefficients for y3, y4 and y5 to use a2. You should get your expected result by simply substituting the respective a3, a4 and a5 like so:
for i = 2:L,
y1(i) = (1-a1)*y1(i-1) + a1*x(i);
y2(i) = (1-a2)*y2(i-1) + a2*x(i)/A;
y3(i) = (1-a3)*y3(i-1) + a3*x(i)/A^2;
y4(i) = (1-a4)*y4(i-1) + a4*x(i)/A^3;
y5(i) = (1-a5)*y5(i-1) + a5*x(i)/A^4;
y(i) = y1(i) + y2(i) + y3(i) + y4(i) + y5(i);
end

matlab: minimization/optimization algorithm

I use function with multiple outputs farina4 that computes coefficients a, b, e, f and a vector out_p5tads_final (1 x n array) through a minimization of a system of equations using the data input set p5tads (1 x n array):
function [a b e f fval out_p5tads_final] = farina4(p5tads)
f = #(coeff)calculs_farina4(coeff,p5tads);
[ans,fval] = fminsearchcon(f,coeff0,[0 0 0 0],[1 1 1 1]);% fminsearch with constrains
a = ans(1);
b = ans(2);
e = ans(3);
f = ans(4);
out_p5tads_final = p5tads_farina4(a,b,e,f);
function out_coeff = calculs_farina4(coeff0,p5tads)
%bla-bla-bla
end
function out_p5tads = p5tads_farina4(a,b,e,f)
%bla-bla-bla
end
end
After calculating a, b, e, f and out_p5tads_final I need to calculate/minimize the RMS function with respect to out_p5tads_f4.
RMS = sqrt(mean((p5tads(:) - out_p5tads_f4(:)).^2))*100
and to repeat function farina4 in order to find the optimal set of the parameters a, b, e, f and out_p5tads_final.
I am trying to build up an algorithm of such optimization and do not see a way so far.
For instance, it seems to be not possible to introduce a function with multiple output inside the above RMS equation unless there is a way to index somehow the output of this function farina4.
If there can be an alternative optimization algorithm for RMS without fminsearch (or similar) ?
a b e and f are values between 0 and 1
out_p5tads_final is an (1 x 10) array
%
function out_coeff = calculs_farina4(coeff0,p5tads)
%
mmmm = p5tads(1);
mmmr = p5tads(2);
rmmr = p5tads(3);
mmrm = p5tads(4);
mmrr = p5tads(5);
rmrm = p5tads(6);
rmrr = p5tads(7);
mrrm = p5tads(8);
mrrr = p5tads(9);
rrrr = p5tads(10);
%
a = coeff0(1);
b = coeff0(2);
e = coeff0(3);
f = coeff0(4);
%
f_mmmm = mmmm - ((a^2*b^2*(a + b) + e^2*f^2*(e + f))/2);
f_mmmr = mmmr - (a^2*b^2*(e + f) + e^2*f^2*(a + b));
f_rmmr = rmmr - ((a^2*f^2*(b + e) + b^2*e^2*(a + f))/2);
f_mmrm = mmrm - 2*a*b*e*f;
f_mmrr = mmrr - b*f*(a^3 + e^3) + a*e*(a^3 + f^3);
f_rmrm = rmrm - 2*a*b*e*f;
f_rmrr = rmrr - 2*a*b*e*f;
f_mrrm = mrrm - ((a^2*b^2*(e + f) + e^2*f^2*(a + b))/2);
f_mrrr = mrrr - (a^2*f^2*(b + e) + b^2*e^2*(a + f));
f_rrrr = rrrr - ((a^2*f^2*(a + f) + b^2*e^2*(b + e))/2);
%
out_coeff = f_mmmm^2 + f_mmmr^2 + f_rmmr^2 + f_mmrm^2 + f_mmrr^2 + f_rmrm^2 + f_rmrr^2 + f_mrrm^2 + f_mrrr^2 + f_rrrr^2;
end
%
function out_p5tads = p5tads_farina4(a,b,e,f)
%
p_mmmm = ((a^2*b^2*(a + b) + e^2*f^2*(e + f))/2);
p_mmmr = (a^2*b^2*(e + f) + e^2*f^2*(a + b));
p_rmmr = ((a^2*f^2*(b + e) + b^2*e^2*(a + f))/2);
p_mmrm = 2*a*b*e*f;
p_mmrr = b*f*(a^3 + e^3) + a*e*(a^3 + f^3);
p_rmrm = 2*a*b*e*f;
p_rmrr = 2*a*b*e*f;
p_mrrm = ((a^2*b^2*(e + f) + e^2*f^2*(a + b))/2);
p_mrrr = (a^2*f^2*(b + e) + b^2*e^2*(a + f));
p_rrrr = ((a^2*f^2*(a + f) + b^2*e^2*(b + e))/2);
%
out_p5tads = [p_mmmm,p_mmmr,p_rmmr,p_mmrm,p_mmrr,p_rmrm,p_rmrr,p_mrrm,p_mrrr,p_rrrr];
end
end
Thanks much in advance !
19/08/2014 3:35 pm
I need to get an optimal set of coefficients a b e f that the RMS value , which is calculated from
RMS = sqrt(mean((p5tads(:) - out_p5tads_f4(:)).^2))*100
is minimal. Here, the vector p5tads is used to calculate/optimize the set of a b e f coefficients, which are in turn used to calculate the vector out_p5tads_f4. The code should run a desired number of optimizations cycles (e.g. by default 100) and then select the series of a b e f and out_p5tads_f4 afforded the minimal RMS error value (with respect to out_p5tads_f4).

Integral Calculation

I would like to perform what follows:
where
The parameters shown in the latter figure can be obtained as follows:
%% Inizialization
time = 614.4; % Analysis Time
Uhub = 11;
HubHt = 90;
alpha = 0.14;
TI = 'A'; % Turbulent Intensity (A,B,C as in the IEC or Specific value)
N1 = 4096;
N2 = 32;
N3 = 32;
N = N1*N2*N3; % Total Number of Point
t = 0:(time/(N1-1)):time; % Sampled Time Vector
L1 = Uhub*time; % Box length along X
L2 = 150; % Box length along Y
L3 = 220; % Box length along Z
dx = L1/N1; % Grid Resolution along X-axis
dy = L2/N2; % Grid Resolution along Y-axis
dz = L3/N3; % Grid Resolution along Z-axis
V = L1*L2*L3; % Analysis Box Volume
gamma = 3.9; % Turbulent Eddies Distorsion Factor
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;
sigma2 = 0.7*sigma1;
sigma3 = 0.5*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);
%% Calculation of beta by means of the Energy Spectrum Integration
E = #(k) (1.453*k.^4)./((1 + k.^2).^(17/6));
%//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
E_int = reshape(E_int,N,1);
S = k.*sqrt(E_int);
beta = (c*gamma)./S;
%% Help Parameters
k30 = k3 + beta.*k1;
k0 = sqrt(k1.^2 + k2.^2 + k30.^2);
C1 = (beta.*k1.^2.*(k1.^2 + k2.^2 - k3.*k30))./(k.^2.*(k1.^2 + k2.^2));
C2 = (k2.*k0.^2./((k1.^2 + k2.^2).^(3/2))).*atan2((beta.*k1.*sqrt(k1.^2 + k2.^2)),(k0.^2 - k30.*k1.*beta));
xhsi1 = C1 - (k2./k1).*C2;
xhsi2 = (k2./k1).*C1 + C2;
E_k0 = (1.453*k0.^4)./((1 + k0.^2).^(17/6));
For instance, typing in
phi_33 = #(k2,k3) (E_k0./(4*pi.*k.^4)).*((k1.^2 + k2.^2));
F_33 = arrayfun(#(i) dblquad(phi_33,k3(i),k3(i+1),k2(i),k2(i+1)), 1:((N1*N2*N3)-1));
Matlab retrieves the following error msg:
Error using +
Matrix dimensions must agree.
Error in #(k2,k3)(E_k0./(4*pi.*k.^4)).*((k1.^2+k2.^2))
Do you have a clue how to overcome this issue?
I really look forward to hearing from you.
Best regards,
FPE
The error is easily explained:
First you define E_k0 then you try to call Ek0.
phi_11 = #(k1,k2,k3) (E_k0./4*pi.*kabs.^4).*(k0abs.^2 - k1.^2 - 2*k1.*k03.*xhsi1 + (k1.^2 + k2.^2).*xhsi1.^2);
I solved it this way:
Code a function for each of the PHI elements, such as (for PHI11)
function phi_11 = phi_11_new(k1,k2,k3,beta,i)
k = sqrt(k1(i).^2 + k2.^2 + k3.^2);
k30 = k3 + beta(i).*k1(i);
k0 = sqrt(k1(i).^2 + k2.^2 + k30.^2);
E_k0 = 1.453.*k0.^4./((1 + k0.^2).^(17/6));
C1 = (beta(i).k1(i).^2).(k1(i).^2 + k2.^2 - k3.k30)./(k.^2.(k1(i).^2 + k2.^2));
C2 = k2.*k0.^2./((k1(i).^2 + k2.^2).^(3/2)).*atan2((beta(i).*k1(i).*sqrt(k1(i).^2 + k2.^2)),(k0.^2 - k30.*k1(i).*beta(i)));
xhsi1 = C1 - k2./k1(i).*C2;
xhsi1_q = xhsi1.^2;
phi_11 = E_k0./(4.*pi.k0.^4).(k0.^2 - k1(i).^2 - 2.*k1(i).*k30.*xhsi1 + (k1(i).^2 + k2.^2).*xhsi1_q);
end
I recall this function within the main code as follows
for l = 1:numel(k1)
phi11 = #(k2,k3) phi_11(k1,k2,k3,l)
F11(l) = integral2(phi,-1000,1000,-1000,1000);
end
at it seems it works.