I have a 3 columns in a table named col1, col2, col3 containing integer values. there are 4000 + rows. there is three parameter #first, #second , #third. I want to get rows who has match according to this parameter. The query is giving results but it is not giving me 3/3 or if there is no match it is not showing and also I want to make it descending order.
My output should be like this
table :
col1 col2 col3
3 4 5
1 2 2
7 7 9
3 2 3
1 8 9
1 2 3
first = 1 second = 2 and third = 3
3 / 3 1 ( as sixth row contains 1, 2, 3)
2 / 3 2 ( as second row and fourth row )
1 / 3 1 (fifth row)
0 / 3 2 (ist , third )
(
SELECT count (CASE WHEN col1 = #First THEN 1 ELSE 0 END +
CASE WHEN col2= #second THEN 1 ELSE 0 END +
CASE WHEN col3 = #third THEN 1 ELSE 0 END
)AS "NUM_OF_MATCHES" ,
CAST( CASE WHEN col1 = #First THEN 1 ELSE 0 END +
CASE WHEN col2= #sec/ond THEN 1 ELSE 0 END +
CASE WHEN col3 = #third THEN 1 ELSE 0 END
AS VARCHAR(10)) + '/ 3'
AS "match"
FrOM dbo.FormsDataRowFormat a
where ( SELECT CASE WHEN col1 = #First THEN 1 ELSE 0 END +
CASE WHEN col2= #second THEN 1 ELSE 0 END +
CASE WHEN col3 = #third THEN 1 ELSE 0 END AS "NUM_OF_MATCHES" ) >= 1
group by ( CASE WHEN col1 = #First THEN 1 ELSE 0 END +
CASE WHEN col2 = #second THEN 1 ELSE 0 END+
CASE WHEN col3 = #third THEN 1 ELSE 0 END )
)
SELECT
'0/3' AS Matches,
SUM(CASE WHEN (CASE WHEN col1 = #first THEN 1 ELSE 0 END +
CASE WHEN col2 = #second THEN 1 ELSE 0 END +
CASE WHEN col3 = #third THEN 1 ELSE 0 END) = 0 THEN 1 ELSE 0 END) AS NumMatches
FROM FormsDataRowFormat
UNION
SELECT '1/3' AS Matches,
SUM(CASE WHEN (CASE WHEN col1 = #first THEN 1 ELSE 0 END +
CASE WHEN col2 = #second THEN 1 ELSE 0 END +
CASE WHEN col3 = #third THEN 1 ELSE 0 END) = 1 THEN 1 ELSE 0 END) AS NumMatches
FROM FormsDataRowFormat
UNION
SELECT '2/3' AS Matches,
SUM(CASE WHEN (CASE WHEN col1 = #first THEN 1 ELSE 0 END +
CASE WHEN col2 = #second THEN 1 ELSE 0 END +
CASE WHEN col3 = #third THEN 1 ELSE 0 END) = 2 THEN 1 ELSE 0 END) AS NumMatches
FROM FormsDataRowFormat
UNION
SELECT '3/3' AS Matches,
SUM(CASE WHEN (CASE WHEN col1 = #first THEN 1 ELSE 0 END +
CASE WHEN col2 = #second THEN 1 ELSE 0 END +
CASE WHEN col3 = #third THEN 1 ELSE 0 END) = 3 THEN 1 ELSE 0 END) AS NumMatches
FROM FormsDataRowFormat
For input data:
/--------------------\
| col1 | col2 | col3 |
|------+------+------|
| 8 | 7 | 1 |
| 5 | 7 | 1 |
| 4 | 4 | 4 |
| 4 | 5 | 2 |
| 4 | 6 | 2 |
| 2 | 2 | 3 |
| 5 | 2 | 1 |
| 2 | 7 | 7 |
| 2 | 1 | 3 |
| 1 | 2 | 3 |
\--------------------/
with:
#first = 1
#second = 2
#third = 3
gives output:
/----------------------\
| Matches | NumMatches |
|---------+------------|
| 0/3 | 6 |
| 1/3 | 2 |
| 2/3 | 1 |
| 3/3 | 1 |
\----------------------/
Edit - Dynamic SQL solution:
Following your comment clarifying that the number of columns is dynamic (between 3 and 6 inclusive), below is a dynamic SQL solution which will handle any such number of columns.
Note however that dynamic SQL is probably not the best solution to this problem, especially if it's something which will be repeated many times and/or runs on very large datasets. However, it's the only way I know to achieve this, so I wanted to include it (and would be very happy to see better solutions from other users!).
DECLARE #numColumns INT
SELECT #numColumns = COUNT(*) FROM sysobjects o JOIN syscolumns c ON o.id = c.id WHERE o.name = 'FormsDataRowFormat'
DECLARE #sql NVARCHAR(MAX)
SET #sql =
'SELECT ' +
'''0/' + CONVERT(VARCHAR,#numColumns) + ''' AS Matches, ' +
'SUM(CASE WHEN (CASE WHEN col1 = ' + CONVERT(VARCHAR,#first) + ' THEN 1 ELSE 0 END + ' +
'CASE WHEN col2 = ' + CONVERT(VARCHAR,#second) + ' THEN 1 ELSE 0 END + ' +
'CASE WHEN col3 = ' + CONVERT(VARCHAR,#third) + ' THEN 1 ELSE 0 END' +
CASE WHEN #numColumns >= 4 THEN ' + CASE WHEN col4 = ' + CONVERT(VARCHAR,#fourth) + ' THEN 1 ELSE 0 END' ELSE '' END +
CASE WHEN #numColumns >= 5 THEN ' + CASE WHEN col5 = ' + CONVERT(VARCHAR,#fifth) + ' THEN 1 ELSE 0 END' ELSE '' END +
CASE WHEN #numColumns = 6 THEN ' + CASE WHEN col6 = ' + CONVERT(VARCHAR,#sixth) + ' THEN 1 ELSE 0 END' ELSE '' END +
') = 0 THEN 1 ELSE 0 END) AS NumMatches ' +
'FROM FormsDataRowFormat ' +
'UNION ' +
'SELECT ''1/' + CONVERT(VARCHAR,#numColumns) + ''' AS Matches, ' +
'SUM(CASE WHEN (CASE WHEN col1 = ' + CONVERT(VARCHAR,#first) + ' THEN 1 ELSE 0 END + ' +
'CASE WHEN col2 = ' + CONVERT(VARCHAR,#second) + ' THEN 1 ELSE 0 END + ' +
'CASE WHEN col3 = ' + CONVERT(VARCHAR,#third) + ' THEN 1 ELSE 0 END' +
CASE WHEN #numColumns >= 4 THEN ' + CASE WHEN col4 = ' + CONVERT(VARCHAR,#fourth) + ' THEN 1 ELSE 0 END' ELSE '' END +
CASE WHEN #numColumns >= 5 THEN ' + CASE WHEN col5 = ' + CONVERT(VARCHAR,#fifth) + ' THEN 1 ELSE 0 END' ELSE '' END +
CASE WHEN #numColumns = 6 THEN ' + CASE WHEN col6 = ' + CONVERT(VARCHAR,#sixth) + ' THEN 1 ELSE 0 END' ELSE '' END +
') = 1 THEN 1 ELSE 0 END) AS NumMatches ' +
'FROM FormsDataRowFormat ' +
'UNION ' +
'SELECT ''2/' + CONVERT(VARCHAR,#numColumns) + ''' AS Matches, ' +
'SUM(CASE WHEN (CASE WHEN col1 = ' + CONVERT(VARCHAR,#first) + ' THEN 1 ELSE 0 END + ' +
'CASE WHEN col2 = ' + CONVERT(VARCHAR,#second) + ' THEN 1 ELSE 0 END + ' +
'CASE WHEN col3 = ' + CONVERT(VARCHAR,#third) + ' THEN 1 ELSE 0 END' +
CASE WHEN #numColumns >= 4 THEN ' + CASE WHEN col4 = ' + CONVERT(VARCHAR,#fourth) + ' THEN 1 ELSE 0 END' ELSE '' END +
CASE WHEN #numColumns >= 5 THEN ' + CASE WHEN col5 = ' + CONVERT(VARCHAR,#fifth) + ' THEN 1 ELSE 0 END' ELSE '' END +
CASE WHEN #numColumns = 6 THEN ' + CASE WHEN col6 = ' + CONVERT(VARCHAR,#sixth) + ' THEN 1 ELSE 0 END' ELSE '' END +
') = 2 THEN 1 ELSE 0 END) AS NumMatches ' +
'FROM FormsDataRowFormat ' +
'UNION ' +
'SELECT ''3/' + CONVERT(VARCHAR,#numColumns) + ''' AS Matches, ' +
'SUM(CASE WHEN (CASE WHEN col1 = ' + CONVERT(VARCHAR,#first) + ' THEN 1 ELSE 0 END + ' +
'CASE WHEN col2 = ' + CONVERT(VARCHAR,#second) + ' THEN 1 ELSE 0 END + ' +
'CASE WHEN col3 = ' + CONVERT(VARCHAR,#third) + ' THEN 1 ELSE 0 END' +
CASE WHEN #numColumns >= 4 THEN ' + CASE WHEN col4 = ' + CONVERT(VARCHAR,#fourth) + ' THEN 1 ELSE 0 END' ELSE '' END +
CASE WHEN #numColumns >= 5 THEN ' + CASE WHEN col5 = ' + CONVERT(VARCHAR,#fifth) + ' THEN 1 ELSE 0 END' ELSE '' END +
CASE WHEN #numColumns = 6 THEN ' + CASE WHEN col6 = ' + CONVERT(VARCHAR,#sixth) + ' THEN 1 ELSE 0 END' ELSE '' END +
') = 3 THEN 1 ELSE 0 END) AS NumMatches ' +
'FROM FormsDataRowFormat ' +
CASE WHEN #numColumns >= 4 THEN
'UNION ' +
'SELECT ''4/' + CONVERT(VARCHAR,#numColumns) + ''' AS Matches, ' +
'SUM(CASE WHEN (CASE WHEN col1 = ' + CONVERT(VARCHAR,#first) + ' THEN 1 ELSE 0 END + ' +
'CASE WHEN col2 = ' + CONVERT(VARCHAR,#second) + ' THEN 1 ELSE 0 END + ' +
'CASE WHEN col3 = ' + CONVERT(VARCHAR,#third) + ' THEN 1 ELSE 0 END' +
CASE WHEN #numColumns >= 4 THEN ' + CASE WHEN col4 = ' + CONVERT(VARCHAR,#fourth) + ' THEN 1 ELSE 0 END' ELSE '' END +
CASE WHEN #numColumns >= 5 THEN ' + CASE WHEN col5 = ' + CONVERT(VARCHAR,#fifth) + ' THEN 1 ELSE 0 END' ELSE '' END +
CASE WHEN #numColumns = 6 THEN ' + CASE WHEN col6 = ' + CONVERT(VARCHAR,#sixth) + ' THEN 1 ELSE 0 END' ELSE '' END +
') = 4 THEN 1 ELSE 0 END) AS NumMatches ' +
'FROM FormsDataRowFormat ' ELSE '' END +
CASE WHEN #numColumns >= 5 THEN
'UNION ' +
'SELECT ''5/' + CONVERT(VARCHAR,#numColumns) + ''' AS Matches, ' +
'SUM(CASE WHEN (CASE WHEN col1 = ' + CONVERT(VARCHAR,#first) + ' THEN 1 ELSE 0 END + ' +
'CASE WHEN col2 = ' + CONVERT(VARCHAR,#second) + ' THEN 1 ELSE 0 END + ' +
'CASE WHEN col3 = ' + CONVERT(VARCHAR,#third) + ' THEN 1 ELSE 0 END' +
CASE WHEN #numColumns >= 4 THEN ' + CASE WHEN col4 = ' + CONVERT(VARCHAR,#fourth) + ' THEN 1 ELSE 0 END' ELSE '' END +
CASE WHEN #numColumns >= 5 THEN ' + CASE WHEN col5 = ' + CONVERT(VARCHAR,#fifth) + ' THEN 1 ELSE 0 END' ELSE '' END +
CASE WHEN #numColumns = 6 THEN ' + CASE WHEN col6 = ' + CONVERT(VARCHAR,#sixth) + ' THEN 1 ELSE 0 END' ELSE '' END +
') = 5 THEN 1 ELSE 0 END) AS NumMatches ' +
'FROM FormsDataRowFormat ' ELSE '' END +
CASE WHEN #numColumns = 6 THEN
'UNION ' +
'SELECT ''6/' + CONVERT(VARCHAR,#numColumns) + ''' AS Matches, ' +
'SUM(CASE WHEN (CASE WHEN col1 = ' + CONVERT(VARCHAR,#first) + ' THEN 1 ELSE 0 END + ' +
'CASE WHEN col2 = ' + CONVERT(VARCHAR,#second) + ' THEN 1 ELSE 0 END + ' +
'CASE WHEN col3 = ' + CONVERT(VARCHAR,#third) + ' THEN 1 ELSE 0 END' +
CASE WHEN #numColumns >= 4 THEN ' + CASE WHEN col4 = ' + CONVERT(VARCHAR,#fourth) + ' THEN 1 ELSE 0 END' ELSE '' END +
CASE WHEN #numColumns >= 5 THEN ' + CASE WHEN col5 = ' + CONVERT(VARCHAR,#fifth) + ' THEN 1 ELSE 0 END' ELSE '' END +
CASE WHEN #numColumns = 6 THEN ' + CASE WHEN col6 = ' + CONVERT(VARCHAR,#sixth) + ' THEN 1 ELSE 0 END' ELSE '' END +
') = 6 THEN 1 ELSE 0 END) AS NumMatches ' +
'FROM FormsDataRowFormat' ELSE '' END
EXEC sp_executesql #sql
after a bit of help please :)
The following MySQL query
select id, customer_id, user,
(((q_responce_time + o_responce_time + cs_responce_time + q_accuracy +
o_accuracy + cs_accuracy + q_personnel + o_personnel + cs_personnel +
q_communication + o_communication + d_communication + cs_communication +
q_overall + qu_overall + o_overall + d_overall + cs_overall + profile +
glass + parts + roof + in_full + direct_delivery + damages + service)/125)*100) as total,
month(create_datetime) as posted_month
from cs_review_centre
Where
create_datetime >= '2013-01-01'
and create_datetime < '2013-10-31'
and customer_id = 26
order by posted_month, customer_id
produces the following result
"customer_id" | "user" |"total" |"posted_month"|
"26" | "co2_test" |"72.8000" | "7" |
"26" | "co2_test" |"60.8000" | "8" |
"26" | "Lisa" |"81.6000" | "9" |
"26" | "Lisa" |"84.0000" | "10" |
"26" | "Lisa" |"52.0000" | "10" |
what I want to achieve is when a posted_month contains a duplicate value I want to average the "total"
Any help would very greatly appreciated
Thanks
Just GROUP BY customer_id, user,posted_month and use AVG() function
select customer_id, user,
AVG(
(((q_responce_time + o_responce_time + cs_responce_time + q_accuracy +
o_accuracy + cs_accuracy + q_personnel + o_personnel + cs_personnel +
q_communication + o_communication + d_communication + cs_communication +
q_overall + qu_overall + o_overall + d_overall + cs_overall + profile +
glass + parts + roof + in_full + direct_delivery + damages + service)/125)*100)
) as AverageTotal,
month(create_datetime) as posted_month
from cs_review_centre
Where
create_datetime >= '2013-01-01'
and create_datetime < '2013-10-31'
and customer_id = 26
GROUP BY customer_id, user,posted_month
order by posted_month, customer_id
Try:
select id, customer_id, user,
avg((((q_responce_time + o_responce_time + cs_responce_time + q_accuracy +
o_accuracy + cs_accuracy + q_personnel + o_personnel + cs_personnel +
q_communication + o_communication + d_communication + cs_communication +
q_overall + qu_overall + o_overall + d_overall + cs_overall + profile +
glass + parts + roof + in_full + direct_delivery + damages + service)/125)*100)) as total,
month(create_datetime) as posted_month
from cs_review_centre
Where
create_datetime >= '2013-01-01'
and create_datetime < '2013-10-31'
and customer_id = 26
group by id, customer_id, user, month(create_datetime)
order by posted_month, customer_id
starting from this code:
clc, clear all, close all
tic
k1 = 0.01:0.1:100;
k2 = 0.01:0.1:100;
k3 = 0.01:0.1:100;
k = sqrt(k1.^2 + k2.^2 + k3.^2);
c = 1.476;
gamma = 3.9;
colors = {'cyan'};
Ek = (1.453*k.^4)./((1 + k.^2).^(17/6));
E = #(k) (1.453*k.^4)./((1 + k.^2).^(17/6));
E_int = zeros(1,numel(k1));
E_int(1) = 1.5;
for i = 2:numel(k)
E_int(i) = E_int(i-1) - integral(E,k(i-1),k(i));
end
beta = c*gamma./(k.*sqrt(E_int));
F_11 = zeros(1,numel(k1));
F_22 = zeros(1,numel(k1));
F_33 = zeros(1,numel(k1));
count = 0;
for i = 1:numel(k1)
count = count + 1;
phi_11 = #(k2,k3) phi_11_new(k1,k2,k3,beta,i);
phi_22 = #(k2,k3) phi_22_new(k1,k2,k3,beta,i);
phi_33 = #(k2,k3) phi_33_new(k1,k2,k3,beta,i);
F_11(count) = integral2(phi_11,-100,100,-100,100);
F_22(count) = integral2(phi_22,-100,100,-100,100);
F_33(count) = integral2(phi_33,-100,100,-100,100);
end
figure
hold on
plot(k1,F_11,'b')
plot(k1,F_22,'cyan')
plot(k1,F_33,'magenta')
hold off
where
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
function phi_22 = phi_22_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)));
xhsi2 = k2./k1(i).*C1 + C2;
xhsi2_q = xhsi2.^2;
phi_22 = E_k0./(4.*pi.*k0.^4).*(k0.^2 - k2.^2 - 2.*k2.*k30.*xhsi2 + (k1(i).^2 + k2.^2).*xhsi2_q);
end
function phi_33 = phi_33_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)));
phi_33 = (E_k0./(4*pi.*(k.^4))).*(k1(i).^2+k2.^2);
end
This procedure is leading me to results not matching some others coming from a study. The results I should match are posted below:
whereas mine look like these
It's quite easy to esteem how only the comp w match the theoretical results; therefore, I believe that the flaw may reside in the definition of beta outside the function phi_11_new (and phi_22_new).
May any of you suggest how to calculate beta within phi_11_new(and phi_22_new) instead than the way I currently do?
I thank you all in advance for supporting.
Best Regards,
fpe
I have improved the interpolation so that it no longer breaks down for small values. It also returns more correct values since it now interpolates the logarithms of the values.
Here is the code, as it is now.
function test15()
[k1,k2,k3] = deal(0.01:0.1:400);
k = sqrt(k1.^2 + k2.^2 + k3.^2);
c = 1.476;
gamma = 3.9;
Ek = (1.453*k.^4)./((1 + k.^2).^(17/6));
E_int = 1.5-cumtrapz(k,Ek);
beta = c*gamma./(k.*sqrt(E_int));
[F_11,F_22,F_33] = deal(zeros(1,numel(k1)));
k_vec = k;
beta_vec = beta;
kLim = 100;
for ii = 1:numel(k1)
phi_11 = #(k2,k3) phi_11_new(k1(ii),k2,k3,k_vec,beta_vec);
phi_22 = #(k2,k3) phi_22_new(k1(ii),k2,k3,k_vec,beta_vec);
phi_33 = #(k2,k3) phi_33_new(k1(ii),k2,k3,k_vec,beta_vec);
F_11(ii) = quad2d(phi_11,-kLim,kLim,-kLim,kLim);
F_22(ii) = quad2d(phi_22,-kLim,kLim,-kLim,kLim);
F_33(ii) = quad2d(phi_33,-kLim,kLim,-kLim,kLim);
end
figure
loglog(k1,F_11,'b')
hold on
loglog(k1,F_22,'cyan')
loglog(k1,F_33,'magenta')
hold off
grid on
end
function phi_11 = phi_11_new(k1,k2,k3,k_vec,beta_vec)
k = sqrt(k1^2 + k2.^2 + k3.^2);
log_beta_vec = interp1(log(k_vec),log(beta_vec),log(k(:)),'linear','extrap');
log_beta = reshape(log_beta_vec,size(k));
beta = exp(log_beta);
k30 = k3 + beta*k1;
k0 = sqrt(k1^2 + k2.^2 + k30.^2);
E_k0 = 1.453*k0.^4./((1 + k0.^2).^(17/6));
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;
xhsi1_q = xhsi1.^2;
phi_11 = E_k0./(4.*pi.*k0.^4).*(k0.^2 - k1^2 - 2*k1*k30.*xhsi1 + (k1^2 + k2.^2).*xhsi1_q);
end
function phi_22 = phi_22_new(k1,k2,k3,k_vec,beta_vec)
k = sqrt(k1^2 + k2.^2 + k3.^2);
log_beta_vec = interp1(log(k_vec),log(beta_vec),log(k(:)),'linear','extrap');
log_beta = reshape(log_beta_vec,size(k));
beta = exp(log_beta);
k30 = k3 + beta*k1;
k0 = sqrt(k1^2 + k2.^2 + k30.^2);
E_k0 = 1.453*k0.^4./((1 + k0.^2).^(17/6));
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));
xhsi2 = (k2/k1).*C1 + C2;
xhsi2_q = xhsi2.^2;
phi_22 = E_k0./(4.*pi.*k0.^4).*(k0.^2 - k2.^2 - 2.*k2.*k30.*xhsi2 + (k1^2 + k2.^2).*xhsi2_q);
end
function phi_33 = phi_33_new(k1,k2,k3,k_vec,beta_vec)
k = sqrt(k1^2+k2.^2+k3.^2);
log_beta_vec = interp1(log(k_vec),log(beta_vec),log(k(:)),'linear','extrap');
log_beta = reshape(log_beta_vec,size(k));
beta = exp(log_beta);
k30 = k3 + beta*k1;
k0 = sqrt(k1^2+k2.^2+k30.^2);
E_k0 = (1.453*k0.^4./((1+k0.^2).^(17/6)));
phi_33 = (E_k0./(4*pi*(k.^4))).*(k1^2+k2.^2);
end
The figure seems to agree with the original result quite well. Even if there still are some differences.
Side note: Since a k-value of 100 is set as an upper limit in the simulation the values greater than this in the figure are incorrect. They are calculated without using all values in the full (k2,k3)-"circle". We can also see a deviation for these values.
Ok, this is what I have got at the moment. I would like to hear what you think about it - it is not perfect yet. I have not access to the functions integral or integral2 so if you can reinsert them (instead of my quad2d for example) and test the code you might get better results than I have now.
My first thought was to calculate beta in a for-loop for every triplet of [k1,k2,k3] in the phi-functions. This turned out to be extremely slow so I have instead used a vector of k-values and calculated the corresponding vector beta just as you did before. These two vectors are then passed to phi where the values are used in an interpolation function (interp1) to find the beta-values of specific k-values.
function myFunction()
[k1,k2,k3] = deal(0.01:0.1:400);
k = sqrt(k1.^2 + k2.^2 + k3.^2);
c = 1.476;
gamma = 3.9;
Ek = (1.453*k.^4)./((1 + k.^2).^(17/6));
E_int = 1.5-cumtrapz(k,Ek);
beta = c*gamma./(k.*sqrt(E_int));
[F_11,F_22,F_33] = deal(zeros(1,numel(k1)));
k_vec = k;
beta_vec = beta;
for ii = 1:numel(k1)
phi_11 = #(k2,k3) phi_11_new(k1(ii),k2,k3,k_vec,beta_vec);
phi_22 = #(k2,k3) phi_22_new(k1(ii),k2,k3,k_vec,beta_vec);
phi_33 = #(k2,k3) phi_33_new(k1(ii),k2,k3,k_vec,beta_vec);
F_11(ii) = quad2d(phi_11,-100,100,-100,100);
F_22(ii) = quad2d(phi_22,-100,100,-100,100);
F_33(ii) = quad2d(phi_33,-100,100,-100,100);
end
figure
loglog(k1,F_11,'b')
hold on
loglog(k1,F_22,'cyan')
loglog(k1,F_33,'magenta')
hold off
grid on
end
function phi_11 = phi_11_new(k1,k2,k3,k_vec,beta_vec)
k = sqrt(k1^2 + k2.^2 + k3.^2);
beta = reshape(interp1(k_vec,beta_vec,k(:)),size(k));
k30 = k3 + beta*k1;
k0 = sqrt(k1^2 + k2.^2 + k30.^2);
E_k0 = 1.453*k0.^4./((1 + k0.^2).^(17/6));
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;
xhsi1_q = xhsi1.^2;
phi_11 = E_k0./(4.*pi.*k0.^4).*(k0.^2 - k1^2 - 2*k1*k30.*xhsi1 + (k1^2 + k2.^2).*xhsi1_q);
end
function phi_22 = phi_22_new(k1,k2,k3,k_vec,beta_vec)
k = sqrt(k1^2 + k2.^2 + k3.^2);
beta = reshape(interp1(k_vec,beta_vec,k(:)),size(k));
k30 = k3 + beta*k1;
k0 = sqrt(k1^2 + k2.^2 + k30.^2);
E_k0 = 1.453*k0.^4./((1 + k0.^2).^(17/6));
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));
xhsi2 = (k2/k1).*C1 + C2;
xhsi2_q = xhsi2.^2;
phi_22 = E_k0./(4.*pi.*k0.^4).*(k0.^2 - k2.^2 - 2.*k2.*k30.*xhsi2 + (k1^2 + k2.^2).*xhsi2_q);
end
function phi_33 = phi_33_new(k1,k2,k3,k_vec,beta_vec)
k = sqrt(k1^2+k2.^2+k3.^2);
beta = reshape(interp1(k_vec,beta_vec,k(:)),size(k));
k30 = k3 + beta*k1;
k0 = sqrt(k1^2+k2.^2+k30.^2);
E_k0 = (1.453*k0.^4./((1+k0.^2).^(17/6)));
phi_33 = (E_k0./(4*pi*(k.^4))).*(k1^2+k2.^2);
end
This produces the following figure. Note that the integration does not succeed for the smallest values of k1.
Edit - A comment on calculating beta within the phi-functions
Since you essentially tried the same thing that I did initially, I have added an example of how I calculated the beta matrix within the phi-functions. Note that this code is so slow that I have never actually run it to completion.
function phi_11 = phi_11_new(k1,k2,k3)
k = sqrt(k1^2 + k2.^2 + k3.^2);
c = 1.476;
gamma = 3.9;
beta = zeros(size(k));
E = #(x) (1.453*x.^4)./((1 + x.^2).^(17/6));
for ii = 1:size(k,1)
for jj = 1:size(k,2)
E_int = 1.5-quad(E,0.001,k(ii,jj));
beta(ii,jj) = c*gamma/(k(ii,jj)*sqrt(E_int));
end
end
k30 = k3 + beta*k1;
k0 = sqrt(k1^2 + k2.^2 + k30.^2);
E_k0 = 1.453*k0.^4./((1 + k0.^2).^(17/6));
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;
xhsi1_q = xhsi1.^2;
phi_11 = E_k0./(4.*pi.*k0.^4).*(k0.^2 - k1^2 - 2*k1*k30.*xhsi1 + (k1^2 + k2.^2).*xhsi1_q);
end
currently I'm running a different version of the above code.
It follows as
clc, clear all, close all
tic
k1 = (0.01:0.1:100);
c = 1.476;
gamma = 3.9;
F_11 = zeros(1,numel(k1));
F_22 = zeros(1,numel(k1));
F_33 = zeros(1,numel(k1));
count = 0;
for i = 1:numel(k1)
count = count + 1;
phi_11 = #(k2,k3) phi_11_new(k1,k2,k3,gamma,i);
phi_22 = #(k2,k3) phi_22_new(k1,k2,k3,gamma,i);
phi_33 = #(k2,k3) phi_33_new(k1,k2,k3,gamma,i);
F_11(count) = integral2(phi_11,-100,100,-100,100);
F_22(count) = integral2(phi_22,-100,100,-100,100);
F_33(count) = integral2(phi_33,-100,100,-100,100);
end
This time phi_11, phi_22 and phi_33 are given as
function phi_11 = phi_11_new(k1,k2,k3,gamma,i)
k = sqrt(k1(i).^2 + k2.^2 + k3.^2);
beta = gamma./((k.^(2/3)).*sqrt(hypergeom([1/3,17/6],4/3,-k.^(-2))));
k30 = k3 + beta.*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).*(k0.^2 - 2.*k30.^2 + beta.*k30.*k1(i))./(k.^2.*(k1(i).^2 + k2.^2));
C2 = k2.*k0.^2./((k1(i).^2 + k2.^2).^(3/2)).*atan2((beta.*k1(i).*sqrt(k1(i).^2 + k2.^2)),(k0.^2 - k30.*k1(i).*beta));
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
function phi_22 = phi_22_new(k1,k2,k3,gamma,i)
k = sqrt(k1(i).^2 + k2.^2 + k3.^2);
beta = gamma./((k.^(2/3)).*sqrt(hypergeom([1/3,17/6],4/3,-k.^(-2))));
k30 = k3 + beta.*k1(i);
k0 = sqrt(k1(i).^2 + k2.^2 + k30.^2);
E_k0 = 1.453.*k0.^4./((1 + k0.^2).^(17/6));
C1 = (beta.*k1(i).^2).*(k0.^2 - 2.*k30.^2 + beta.*k1(i).*k30)./(k.^2.*(k1(i).^2 + k2.^2));
C2 = k2.*k0.^2./((k1(i).^2 + k2.^2).^(3/2)).*atan2((beta.*k1(i).*sqrt(k1(i).^2 + k2.^2)),(k0.^2 - k30.*k1(i).*beta));
xhsi2 = (k2./k1(i)).*C1 + C2;
xhsi2_q = xhsi2.^2;
phi_22 = E_k0./(4.*pi.*k0.^4).*(k0.^2 - k2.^2 - 2.*k2.*k30.*xhsi2 + (k1(i).^2 + k2.^2).*xhsi2_q);
end
function phi_33 = phi_33_new(k1,k2,k3,gamma,i)
k = sqrt(k1(i).^2+k2.^2+k3.^2);
beta = gamma./((k.^(2/3)).*sqrt(hypergeom([1/3,17/6],4/3,-k.^(-2))));
k30 = k3 + beta.*k1(i);
k0 = sqrt(k1(i).^2+k2.^2+k30.^2);
E_k0 = (1.453.*k0.^4./((1+k0.^2).^(17/6)));
phi_33 = (E_k0./(4*pi.*(k.^4))).*(k1(i).^2+k2.^2);
end
Please notice that now beta is calculated within the phi functions. Furthermore, I'm using an equivalent expression of beta. For more details, check the picture below out
Hence, in the updated model, I'm calling hypergeom which is rather slow performing.
That's the main reason why I'd like to calculate beta as in the first code, by means of the energy spectrum integral.
Btw, at the moment I have no idea how to perfrom this successfully.
Best regards,
fpe