Find and replace subscripts and superscripts in MS Excel - html

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

Related

tp = torch.sum(temp_target[...,:-1] * temp_inputs, axis=[0,1]), What does temp_target[...,:-1] mean?

When I was learning diceloss, I didn't understand how to write this code:
tp = torch.sum(temp_target[...,:-1] * temp_inputs, axis=[0,1])
What does temp_target[...,:-1] mean?
The code for the entire diceloss calculation is as follows:
def Dice_loss(inputs, target, beta=1, smooth = 1e-5):
n, c, h, w = inputs.size()
nt, ht, wt, ct = target.size()
temp_inputs = torch.softmax(inputs.transpose(1, 2).transpose(2, 3).contiguous().view(n, -1, c),-1)
temp_target = target.view(n, -1, ct)
tp = torch.sum(temp_target[...,:-1] * temp_inputs, axis=[0,1])
fp = torch.sum(temp_inputs , axis=[0,1]) - tp
fn = torch.sum(temp_target[...,:-1] , axis=[0,1]) - tp
score = ((1 + beta ** 2) * tp + smooth) / ((1 + beta ** 2) * tp + beta ** 2 * fn + fp + smooth)
dice_loss = 1 - torch.mean(score)
return dice_loss

Simplify Boolean Expression: X + X'Y'Z

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

How to calculate checksum digit for EAN-14?

I am trying to validate EAN 14 UPC code in vba access. I am trying to find it online but no luck. I just found for EAN 8 and EAN 13. So, I just tried to code it similar to EAN 13 as following:
If Len(Barcode) = 14 Then
'do the check digit for EAN 14 for anything 14 long
checkDigitSubtotal = (Val(Mid(Barcode, 2, 1))) _
+ (Val(Mid(Barcode, 4, 1))) _
+ (Val(Mid(Barcode, 6, 1))) _
+ (Val(Mid(Barcode, 8, 1))) _
+ (Val(Mid(Barcode, 10, 1))) _
+ (Val(Mid(Barcode, 12, 1)))
checkDigitSubtotal = (3 * checkDigitSubtotal) _
+ (Val(Mid(Barcode, 1, 1))) _
+ (Val(Mid(Barcode, 3, 1))) _
+ (Val(Mid(Barcode, 5, 1))) _
+ (Val(Mid(Barcode, 7, 1))) _
+ (Val(Mid(Barcode, 9, 1))) _
+ (Val(Mid(Barcode, 11, 1))) _
+ (Val(Mid(Barcode, 13, 1)))
If Right(Str(300 - checkDigitSubtotal), 1) <> Right(Barcode, 1) Then
Validate_UPC = "EAN14-BAD"
Exit Function
End If
Validate_UPC = "EAN14-GOOD"
Exit Function
End If
It is not working. Issue I am having is although i enter valid EAN, it gives me EAN14-BAD. I think my validating code is not working. I just added last line
+ (Val(Mid(Barcode, 13, 1)))
on EAN13 validation code. Please help.
It worked when I switched multiplying odd ones by 3 as following:
If Len(Barcode) = 14 Then
checkDigitSubtotal = (Val(Mid(Barcode, 1, 1))) _
+ (Val(Mid(Barcode, 3, 1))) _
+ (Val(Mid(Barcode, 5, 1))) _
+ (Val(Mid(Barcode, 7, 1))) _
+ (Val(Mid(Barcode, 9, 1))) _
+ (Val(Mid(Barcode, 11, 1))) _
+ (Val(Mid(Barcode, 13, 1)))
checkDigitSubtotal = (3 * checkDigitSubtotal) _
+ (Val(Mid(Barcode, 2, 1))) _
+ (Val(Mid(Barcode, 4, 1))) _
+ (Val(Mid(Barcode, 6, 1))) _
+ (Val(Mid(Barcode, 8, 1))) _
+ (Val(Mid(Barcode, 10, 1))) _
+ (Val(Mid(Barcode, 12, 1)))
If Right(Str(300 - checkDigitSubtotal), 1) <> Right(Barcode, 1) Then
Validate_UPC = "EAN14-BAD"
Exit Function
End If
Validate_UPC = "EAN14-GOOD"
Exit Function
End If
Did you try with 8 characters?
If Len(Barcode) = 8 Then
'do the check digit for EAN 8 for anything 8 long
checkDigitSubtotal = (Val(Mid(Barcode, 2, 1))) _
+ (Val(Mid(Barcode, 4, 1))) _
+ (Val(Mid(Barcode, 6, 1)))
checkDigitSubtotal = (3 * checkDigitSubtotal) _
+ (Val(Mid(Barcode, 1, 1))) _
+ (Val(Mid(Barcode, 3, 1))) _
+ (Val(Mid(Barcode, 5, 1))) _
+ (Val(Mid(Barcode, 7, 1)))
If Right(Str(300 - checkDigitSubtotal), 1) <> Right(Barcode, 1) Then
Validate_UPC = "EAN8-BAD"
Exit Function
End If
Validate_UPC = "EAN8-GOOD"
Exit Function
End If
EAN8 AND EAN13 IN VB.NET
Public Function generateEAN(ByVal barcode As String, EsEan8 As Boolean) As String
Dim first As Integer = 0
Dim second As Integer = 0
If EsEan8 Then
barcode = Right(("00000000" & barcode), 7)
Else
barcode = Right(("000000000000" & barcode), 12)
End If
If barcode.Length() = 7 OrElse barcode.Length() = 12 Then
For counter As Integer = 0 To barcode.Length() - 1 Step 2
first = (first + barcode.Substring(counter, 1))
If counter + 1 < barcode.Length Then
second = (second + barcode.Substring(counter + 1, 1))
End If
Next
If EsEan8 Then
first = first * 3
Else
second = second * 3
End If
Dim total As Integer = second + first
Dim roundedNum As Integer = (10 - (total Mod 10)) Mod 10
barcode = barcode & roundedNum
End If
Return barcode
End Function

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;

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