Group Abstract Group Abstract

Message Boards Message Boards

No output when optimising an integration function

Posted 1 year ago

I am trying to run this code on mathematica. It runs the code but doesn't produce any output. What am I doing wrong.
PS: I am new and any help will be appreciated.

(* Prompt user to enter the value of m *)
mInput = Input["Enter the value of m: "];
mValues = If[NumericQ[mInput], {mInput}, {}];

(* Check if the input is valid *)
If[Length[mValues] == 0 || mValues[[1]] <= 0 || mValues[[1]] >= 1,
  Print["Invalid input for m. Please enter a value between 0 and 1."],

  (* Initialize arrays to store the values of k *)
  k1Values = {};
  k2Values = {};
  k3Values = {};

  For[i = 1, i <= Length[mValues], i++,
Eab[a, b] := mValues[[i]] * NIntegrate[a, {b, 0, mValues[[i]]}, {a, mValues[[i]], 1 + mValues[[i]]}] / NIntegrate[1, {b, 0, mValues[[i]]}, {a, mValues[[i]], 1 + mValues[[i]]}] + (1 - mValues[[i]])*NIntegrate[a, {b, mValues[[i]],1}, {a, b, 1 + mValues[[i]]}] / NIntegrate[1, {b, mValues[[i]], 1}, {a, b, 1 + mValues[[i]]}];
    Eba[b, a] := (1 - mValues[[i]]) * NIntegrate[b, {a, mValues[[i]], 1}, {b, 0, a}] / NIntegrate[1, {a, mValues[[i]], 1}, {b, 0, a}] +  (mValues[[i]])*NIntegrate[b, {a, mValues[[i]], 1 + mValues[[i]]}, {b, 0, 1}] / NIntegrate[1, {a, mValues[[i]], 1 + mValues[[i]]}, {b, 0, 1}];
    Eaab[a, b] := NIntegrate[a, {b, mValues[[i]], 1}, {a, mValues[[i]], b}] / NIntegrate[1, {b, mValues[[i]], 1}, {a, mValues[[i]], b}];
    Ebba[b, a] := NIntegrate[b, {a, mValues[[i]], 1}, {b, a, 1}] / NIntegrate[1, {a, mValues[[i]], 1}, {b, a, 1}];

    xFunction[k_] := x /. First@Solve[Sqrt[k] * Eab[a, b] * (b-x) + Sqrt[1 - k] * Eba[b, a] == Sqrt[k] * Ebba[b, a] + Sqrt[1 - k] * Eaab[a, b] * (b-x), x];

    r1[a_, b_] := Sqrt[k] * a * a + Sqrt[1 - k] * b * b;
    r2[a_, b_] := Sqrt[k] * b * b + Sqrt[1 - k] * a * a;
    r3[a_, b_] := Sqrt[k] * Eab[a, b] * a + Sqrt[1 - k] * Eba[b, a] * b;
    r4[a_, b_] := Sqrt[k] * Ebba[b, a] * b + Sqrt[1 - k] * Eaab[a, b] * a;

    integral4 = Integrate[r1[a, b], {b, 0, mValues[[i]]}, {a, mValues[[i]], 1 + mValues[[i]]}];
    integral5 = Integrate[r1[a, b], {b, mValues[[i]], 1}, {a, b, 1 + mValues[[i]]}];
    integral6 = Integrate[r2[a, b], {b, mValues[[i]], 1}, {a, mValues[[i]], b}];

    integral7 = Integrate[r3[a, b], {b, 0, mValues[[i]]}, {a, mValues[[i]], 1 + mValues[[i]]}];
    integral8 = Integrate[r3[a, b], {b, mValues[[i]], 1}, {a, b-xFunction[k], 1 + mValues[[i]]}];
    integral9 = Integrate[r4[a, b], {b, mValues[[i]], 1}, {a, mValues[[i]], b - xFunction[k]}];

    eqn2 = integral7 + integral8 + integral9;
    eqn3 = integral4 + integral5 + integral6;

    sol2 = k /. First@Solve[D[eqn2, k] == 0, k];
    sol3 = k /. First@Solve[D[eqn3, k] == 0, k];

filtered_k2 = Pick[sol2, 1/2 <= # <= 1 &];
filtered_k3 = Pick[sol3, 1/2 <= # <= 1 &];

(* Print the filtered solutions *)
Print["Filtered k2 solutions:", filtered_k2];
Print["Filtered k3 solutions:", filtered_k3];

  ]

]
POSTED BY: Anne Shirley
2 Replies
POSTED BY: Ankit Kantheti

I found some problems with your code.

In Solve[D[eqn2, k] == 0, k] the equation is quite difficult to solve exactly. I would suggest NSolve instead of Solve.

When you write filtered_k2, it is interpreted as a pattern. I would suggest a symbol like filteredk2.

The syntax of Pick wants the first element to be a list, but sol2 is a single number, not a list.

The syntax of Pick wants the second element to be a list of True/False, not a function.

POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard