Simplify Boolean Expression: X + X'Y'Z - boolean-logic

I know that the following is equal:
X + X'Y'Z = X + Y'Z
How can simplify the left side to arrive the right side using basic Boolean identities?
Thanks in advance.

Expression Justification
--------------------------------- -------------------------
X + X'Y'Z initial expression
(XY'Z + X(Y'Z)') + X'Y'Z r = rs + rs'
(XY'Z + XY'Z + X(Y'Z)') + X'Y'Z r = r + r
(XY'Z + X(Y'Z)' + XY'Z) + X'Y'Z r + s = s + r
(XY'Z + X(Y'Z)') + (XY'Z + X'Y'Z) (r + s) + t = r + (s + t)
X(Y'Z + (Y'Z)') + (Y'Z)(X + X') rs + rt = r(s + t)
X(1) + (Y'Z)(1) r + r' = 1
X + Y'Z r(1) = r

The fastest way to prove this expression is to add a redundant term that will discard X'
X + X'Y'Z = X(1+Y'Z) + X'Y'Z
= X + XY'Z + X'Y'Z
= X + (X+X')Y'Z
= X + Y'Z

Related

Find and replace subscripts and superscripts in MS Excel

I would like to find all numbers and formulas which contain subscripts and superscripts in excel cells and replace it with html tags for subscripts and superscripts.
Eg. cell containing a2 + (b3 - c) would be replaced as:
a<sup>2</sup> + (b<sup>3</sup> - c)
Thanks a lot.
Try this one:
Sub test()
Dim ColNo, RowNo As Long
Dim NewStr As String
Set ws1 = Worksheets("q")
Set ws2 = Worksheets("q-a")
ws1.Activate
With ws1
RowNo = .UsedRange.SpecialCells(xlCellTypeLastCell).Row
ColNo = .UsedRange.SpecialCells(xlCellTypeLastCell).Column
For i = 1 To ColNo
For j = 1 To RowNo
l = 1
NewStr = .Cells(j, i).Value2
For k = 1 To Len(.Cells(j, i).Value2) - 1
If .Cells(j, i).Characters(k + 1, 1).Font.Superscript = True Then
NewStr = Mid(NewStr, 1, k - 1 + l) & "<sup>" & Mid(.Cells(j, i) _
.Value2, k + 1, 1) & "</sup>" & Mid(.Cells(j, i).Value2, _
k + 2, Len(.Cells(j, i).Value2) - (k + 1))
l = l + 11
ElseIf .Cells(j, i).Characters(k + 1, 1).Font.Subscript = True Then
NewStr = Mid(NewStr, 1, k - 1 + l) & "<sub>" & Mid(.Cells(j, i)._
Value2, k + 1, 1) & "</sub>" & Mid(.Cells(j, i).Value2, _
k + 2, Len(.Cells(j, i).Value2) - (k + 1))
l = l + 11
End If
Next
l = 1
For k = 1 To Len(NewStr) - 1
If InStr(k, NewStr, "</sup><sup>", vbBinaryCompare) = k Then
NewStr = Mid(NewStr, 1, k - 1) & Mid(NewStr, k + 11, Len(NewStr) - (k + 10))
ElseIf InStr(1, NewStr, "</sub><sub>", vbBinaryCompare) <> 0 Then
NewStr = Mid(NewStr, 1, k - 1) & Mid(NewStr, k + 11, Len(NewStr) - (k + 10))
End If
Next
ws2.Cells(j, i).Value2 = NewStr
NewStr = ""
Next
Next
End With
End Sub
According with the new information, I just make it simpler.
Hope it helps
As far as I try, that one works always but fail on the first character. So if the firs character is Subscript or Superscript, it will fail.
The code it is for the superscript case. For the subscript case, just change .Font.Subscript by .Font.Superscript and whatever code on html (<sup> on the superscript).
Sub test()
Dim ColNo, RowNo As Long
Dim Pos(500) As Integer
Dim Str(500) As String
Dim sType(500) as String
Dim NewStr As String
Set ws1 = Worksheets("Hoja1")
Set ws2 = Worksheets("Hoja2")
ws1.Activate
With ws1
RowNo = .UsedRange.SpecialCells(xlCellTypeLastCell).Row
ColNo = .UsedRange.SpecialCells(xlCellTypeLastCell).Column
l = 2
For i = 1 To ColNo
For j = 1 To RowNo
Pos(1) = 1
For k = 1 To Len(.Cells(j, i).Value2) - 1
If .Cells(j, i).Characters(k + 1, 1).Font.Superscript = True Then
Pos(l) = k + 1
sType(l) = "Sup"
l = l + 1
ElseIf .Cells(j, i).Characters(k + 1, 1).Font.Subscript = True Then
Pos(l) = k + 1
sType(l) = "Sub"
l = l + 1
End If
Next
For k = 1 To l - 1
If Pos(k + 1) > Pos(k) Then
If sType(l + 1) = "Sup" Then
Str(2 * k) = Mid(.Cells(j, i).Value2, Pos(k), Pos(k + 1) - Pos(k))
Str(2 * k - 1) = Mid(.Cells(j, i).Value2, Pos(k + 1), 1)
Str(2 * k - 1) = "<sup>" & Str(2 * k - 1) & "</sup>"
NewStr = NewStr & Str(2 * k) & Str(2 * k - 1)
ElseIf sType(l + 1) = "Sub" Then
Str(2 * k) = Mid(.Cells(j, i).Value2, Pos(k), Pos(k + 1) - Pos(k))
Str(2 * k - 1) = Mid(.Cells(j, i).Value2, Pos(k + 1), 1)
Str(2 * k - 1) = "<sub>" & Str(2 * k - 1) & "</sub>"
NewStr = NewStr & Str(2 * k) & Str(2 * k - 1)
End If
End If
Next
If NewStr <> "" Then
NewStr = NewStr + Mid(.Cells(j, i).Value2, _
Pos(l - 1), Len(.Cells(j, i).Value2) - Pos(l - 1))
Else
NewStr = .Cells(j, i).Value2
End If
ws2.Cells(j, i).Value2 = NewStr
NewStr = ""
For k = 1 To l - 1
Pos(k) = 0
sType(k) = ""
Str(2 * k) = ""
Str(2 * k - 1) = ""
Next
l = 2
Next
Next
End With
End Sub
Hope it helps

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

MySQL sun columns only if the comlums contain 1

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;

SUM selected columns using alias and group by

I'm trying to perform calculations on select columns using aliases and a grouping by. Query is below(problem on line before the from):
select r.res_id,
r.arrive_date,
r.depart_date,
r.res_type,
if(DATEDIFF(r.depart_date, r.arrive_date) >29, 'LT', 'ST') as 'StayType',
SUM(r.rent + r.fee_arr_early + r.fee_dep_late + r.fee_peace_waiver + r.fee_pool + r.city_tax + r.fee_cleaning + r.fee_pet + r.fee_tshirt + r.fee_misc + r.fee_non_tax + r.fee_processing + r.fee_travel_ins + r.fee_event + r.fee_cancel) as 'folioTotal',
coalesce((select SUM(g.amount) from guest_payments as g where g.resId = r.res_id and charge_type = 'charge' and approved = 'Y'),0) as 'payments',
coalesce((select SUM(g.amount) from guest_payments as g where g.resId = r.res_id and charge_type = 'credit' and approved = 'Y'),0) as 'credits',
(SUM('folioTotal') - SUM('payments') + SUM('credits')) as 'folioBalance'
from reservations as r
join guest_payments as g
on r.res_id = g.resId
group by r.res_id
I've tried putting this inside another sum with the same outcome.
I was being stupid, I was referencing the aliases inside single ticks which is why it wasn't calculating. Solution:
select r.res_id,
r.arrive_date,
r.depart_date,
r.res_type,
g.entry_date,
if(DATEDIFF(r.depart_date, r.arrive_date) >29, 'LT', 'ST') as 'StayType',
(select SUM(r.rent + r.fee_arr_early + r.fee_dep_late + r.fee_peace_waiver + r.fee_pool + r.city_tax + r.fee_cleaning + r.fee_pet + r.fee_tshirt + r.fee_misc + r.fee_non_tax + r.fee_processing + r.fee_travel_ins + r.fee_event + r.fee_cancel) from reservations as r where r.res_id = g.resId) as 'folioTotal',
coalesce((select SUM(g.amount) from guest_payments as g where g.resId = r.res_id and charge_type = 'charge' ),0) as 'payments',
coalesce((select SUM(g.amount)* -1 from guest_payments as g where g.resId = r.res_id and charge_type = 'credit' and approved = 'Y'),0) as 'credits',
(select folioTotal - payments + credits)
from reservations as r
join guest_payments as g
on r.res_id = g.resId
group by r.res_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