MySQL sun columns only if the comlums contain 1 - mysql

I am tring to construct a select query that sums the total of each column only if the column contains 1 (one).
This is what I have so far:
SELECT
UniqueID,
SUM(
SeqID0101 + SeqID0102 + SeqID0103 + SeqID0104 + SeqID0105 + SeqID0106 + SeqID0107 + SeqID0108 + SeqID0109 + SeqID0110 +
SeqID0201 + SeqID0202 + SeqID0203 +
SeqID0301 + SeqID0302+ SeqID0303 + SeqID0304 +
SeqID0401 + SeqID0402 + SeqID0403 + SeqID0404 +
SeqID0501 + SeqID0502 +
SeqID0601 + SeqID0602 +
SeqID0701 + SeqID0702 +
SeqID0801 + SeqID0802 +
SeqID0901 + SeqID0902 + SeqID0903 +
SeqID1001 + SeqID1002 + SeqID1003 + SeqID1004)
WHERE
(SeqID0101 = 1 OR SeqID0102 = 1 OR SeqID0103 = 1 OR SeqID0104 = 1 OR SeqID0105 = 1 OR SeqID0106 = 1 OR SeqID0107 = 1 OR SeqID0108 = 1 OR SeqID0109 = 1 OR SeqID0110 = 1 OR SeqID0201 = 1 OR SeqID0202 = 1 OR SeqID0203 = 1 OR SeqID0301 = 1
OR SeqID0302 = 1 OR SeqID0303 = 1 OR SeqID0304 = 1 OR SeqID0401 = 1 OR SeqID0402 = 1 OR SeqID0403 = 1 OR SeqID0404 = 1 OR SeqID0501 = 1 OR SeqID0502 = 1 OR SeqID0601 = 1 OR SeqID0602 = 1 OR SeqID0701 = 1 OR SeqID0702 = 1 OR SeqID0801 = 1
OR SeqID0802 = 1 OR SeqID0901 = 1 OR SeqID0902 = 1 OR SeqID1001 = 1 OR SeqID1002 = 1 OR SeqID1003 = 1)
The issue I am having is, if a column contant 3, that 3 is getting included in the SUM total.
Am I doing this wrong and if so how should I construct the query.
Many thanks in advance for your time.
Cheers.

Your text is a bit unclear, but you should be able to use this:
SELECT
UniqueID,
SUM(IF(SeqID0101 = 1, 1, 0)
+ IF(SeqID0102 = 1, 1, 0)
...
+ IF(SeqID1003 = 1, 1, 0))
FROM
my_table
GROUP BY
UniqueID;

Related

Count value and group by type and date

i have this table
(date format : dd/mm/yyyy)
+ order_id + buyer + date + status +
+----------+-------+------------+-------------+
+ CSR001 + AAA + 01/01/2020 + delivered +
+ CSR002 + AAA + 03/01/2020 + canceled +
+ CSR003 + BBB + 01/01/2020 + delivered +
+ CSR004 + BBB + 04/01/2020 + delivered +
+ CSR005 + AAA + 01/02/2020 + canceled +
+ CSR006 + BBB + 01/02/2020 + delivered +
+ CSR007 + AAA + 01/02/2020 + delivered +
+ CSR008 + AAA + 01/02/2020 + delivered +
+----------+-------+------------+-------------+
what is the query to create view in mysql and make it like this, (Group By buyer, status, and count how many of delivered/cancelled value every month)
+ buyer + status + january + february +
+----------+-----------+-----------+------------+
+ AAA + delivered + 1 + 2 +
+ AAA + cancelled + 1 + 1 +
+ BBB + delivered + 2 + 0 +
+ BBB + cancelled + 0 + 0 +
+----------+-----------+-----------+------------+
You can have condition inside count to differentiate the month:
SELECT t.buyer,
t.status,
COUNT(IF(MONTH(t.date) = 1, 1, NULL)) AS jan,
COUNT(IF(MONTH(t.date) = 2, 1, NULL)) AS feb
FROM TABLE AS t
GROUP BY buyer,
status;

mysql average of best three

I have pieced this together from sites online and it works but not completely, what i need it to do is take the top 3 results and average them but it takes ALL results, can anyone point me in the right direction?
SELECT i.NAME,
e.comp,
Round(Avg(c.phase1 + c.phase2 + c.phase3 + c.phase4 + c.phase5
+ c.phase6), 2) AS "Average Score",
( CASE
WHEN compID = '7' THEN Concat(Round(Avg(
( (
c.phase1 + c.phase2 + c.phase3 + c.phase4 + c.phase5
+ c.phase6 ) / 400 ) * 100), 2), ' %')
WHEN compID = '5' THEN Concat(Round(Avg(
( (
c.phase1 + c.phase2 + c.phase3 + c.phase4 + c.phase5
+ c.phase6 ) / 600 ) * 100), 2), ' %')
WHEN compID = '3' THEN
Concat(Round(Avg(( ( c.phase1 + c.phase2 + c.phase3 + c.phase4 + c.phase5
+ c.phase6 ) / 600 ) * 100), 2), ' %')
ELSE 'Unspecified'
END ) AS "Average as Percent"
FROM jos_practicedetail c,
jos_comps e,
jos_practice g,
jos_members i
WHERE e.compsid = g.competition
AND g.practiceid = c.practicepid
AND i.memberid = c.competitorid
AND g.typeID = '2'
AND Year(g.pdate) = '2017'
AND (SELECT Count(*)
FROM jos_practicedetail b
WHERE b.competitorid = c.competitorid
AND b.practicepid = c.practicepid
AND ( b.phase1 + b.phase2 + b.phase3 + b.phase4 + b.phase5
+ b.phase6 ) >= (
c.phase1 + c.phase2 + c.phase3 + c.phase4 + c.phase5
+ c.phase6 )) <= 3
GROUP BY competitorid
HAVING Count(*) > 2
ORDER BY competitorid,
( Avg(c.phase1 + c.phase2 + c.phase3 + c.phase4 + c.phase5
+ c.phase6) ) DESC

SQL Server order desc and also show 6/6

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

MySQL query inc AVG

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

Construct a function

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