Message Boards Message Boards

No output when optimising an integration function

Posted 1 month 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 1 month ago

I found a couple of red flags in your code.Consider using these points.

  1. Input Validation:
  • Ensure that the Input function is working as expected to receive user input for the value of m.
  • Check if the condition for the validity of m values is correctly evaluated.
  1. Function Definitions:
  • Confirm that the functions Eab, Eba, Eaab, Ebba, xFunction, r1, r2, r3, and r4 are defined properly and are handling the symbolic variables and numeric values appropriately.
  1. Integration and Symbolic Computations:
  • Check if the integrals integral4 through integral9 are computed correctly, considering the ranges and variables involved.
  • Ensure that symbolic computations involving Solve and Integrate functions are executed without errors and provide meaningful results.
  1. Solving Equations:
  • Verify that the equations eqn2 and eqn3 are formulated correctly and are solvable within the given context.
  • Confirm that the solutions obtained for k (sol2 and sol3) are meaningful and within the expected range.
  1. Printing Output:
  • Check if the filtered solutions filtered_k2 and filtered_k3 are being computed correctly and printed appropriately.

Reviewing and debugging these aspects of your code should help you identify the specific issues causing the lack of output. also, try breaking down your code into smaller, testable components to isolate and troubleshoot individual parts more effectively.

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

Group Abstract Group Abstract