How to call a built function in mathematica

  • Context: Undergrad 
  • Thread starter Thread starter Reem Hashem
  • Start date Start date
  • Tags Tags
    Function Mathematica
Click For Summary

Discussion Overview

The discussion revolves around how to properly call a user-defined function in Mathematica, specifically in the context of using it with the FindRoot function. Participants explore issues related to function definition, usage of parameters, and troubleshooting errors in the code.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant expresses difficulty in calling their defined function within another function in Mathematica.
  • Another participant suggests simplifying the code to identify errors more easily.
  • A participant shares a complex function definition using NDSolve and provides an example of its output.
  • There is a request for guidance on how to use the defined function with FindRoot, indicating that the initial attempt did not work.
  • A participant emphasizes the importance of understanding the use of underscores in function definitions and provides a link to Mathematica documentation.
  • Suggestions are made to plot the function to understand its behavior better, and there are indications that attempts to use FindRoot have encountered issues that remain unresolved.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to resolve the issues with the function calls, and multiple viewpoints on troubleshooting and usage remain present.

Contextual Notes

There are unresolved issues regarding the specific errors encountered when using FindRoot, and the discussion includes various assumptions about function definitions and parameter usage that may not be fully clarified.

Reem Hashem
Messages
23
Reaction score
0
Hello,I need somebody to help me in Mathematica programm
I wrote code that works as a function which I can call when I need in another function, but it did not work ,so I need help how to call my function
and thanks in advance
 

Attachments

Physics news on Phys.org
Your code is much too complicated. You need to break it down into simpler steps, and then try it on simpler examples, to find where the error lies.
 
In[1]:= tmp[a_, z_, w_] := Module[{m, f, d, b},
m = NDSolve[{
y1'[t] == y5[t], y2'[t] == y6[t], y3'[t] == y7[t], y4'[t] == y8[t],
y5'[t] == y9[t], y6'[t] == y10[t], y7'[t] == y11[t], y8'[t] == y12[t],
y9'[t] == y13[t], y10'[t] == y14[t], y11'[t] == y15[t], y12'[t] == y16[t],
y13'[t] == -z^4 y1[t] + 2 z^2 y9[t] + z^2 w (-t + t^2) y17[t] + a y9[t],
y14'[t] == -z^4 y2[t] + 2 z^2 y10[t] + z^2 w (-t + t^2) y18[t] + a y10[t],
y15'[t] == -z^4 y3[t] + 2 z^2 y11[t] + z^2 w (-t + t^2) y19[t] + a y11[t],
y16'[t] == -z^4 y4[t] + 2 z^2 y12[t] + z^2 w (-t + t^2) y20[t] + a y12[t],
y17'[t] == y21[t], y18'[t] == y22[t], y19'[t] == y23[t], y20'[t] == y24[t],
y21'[t] == (-1 + 2 t) y1[t] + z^2 y17[t] - a y29[t],
y22'[t] == (-1 + 2 t) y2[t] + z^2 y18[t] - a y30[t],
y23'[t] == (-1 + 2 t) y3[t] + z^2 y19[t] - a y31[t],
y24'[t] == (-1 + 2 t) y4[t] + z^2 y20[t] - a y32[t],
y25'[t] == y29[t], y26'[t] == y30[t], y27'[t] == y31[t], y28'[t] == y32[t],
y29'[t] == -y21[t] + z^2 y25[t], y30'[t] == -y22[t] + z^2 y26[t],
y31'[t] == -y23[t] + z^2 y27[t], y32'[t] == -y24[t] + z^2 y28[t],
y1[0] == y2[0] == y3[0] == y4[0] == y5[0] == y6[0] == y7[0] == y8[0] ==
y10[0] == y11[0] == y12[0] == y13[0] == y15[0] == y16[0] == y17[0] ==
y18[0] == y19[0] == y20[0] == y21[0] == y22[0] == y24[0] == y25[0] ==
y26[0] == y27[0] == y28[0] == y29[0] == y30[0] == y31[0] == 0,
y9[0] == y14[0] == y23[0] == y32[0] == 1}, {y1, y2, y3, y4, y5,
y6, y7, y8, y9, y10, y11, y12, y13, y14, y15, y16, y17, y18,
y19, y20, y21, y22, y23, y24, y25, y26, y27, y28, y29, y30, y31,
y32}, {t, 0, 1}];
f[1, 1] = y1[1] /. m;
f[1, 2] = y2[1] /. m;
f[1, 3] = y3[1] /. m;
f[1, 4] = y4[1] /. m;
f[2, 1] = y5[1] /. m;
f[2, 2] = y6[1] /. m;
f[2, 3] = y7[1] /. m;
f[2, 4] = y8[1] /. m;
f[3, 1] = y17[1] /. m;
f[3, 2] = y18[1] /. m;
f[3, 3] = y19[1] /. m;
f[3, 4] = y20[1] /. m;
f[4, 1] = y25[1] /. m;
f[4, 2] = y26[1] /. m;
f[4, 3] = y27[1] /. m;
f[4, 4] = y28[1] /. m;
d = Array[f, {4, 4}];
d = Partition[Flatten[d], 4];
b = Det[d]];
tmp[0, 3.7, 1000]

Out[2]= 15.7127
 
Thank you Mr Bill for your Help ,this what I need,but now I want to know how to call this function in FindRoot function to get the value of w,I tried to do it but it did not work,I wort FindRoot[temp[0,3.7,w_],{w,1000,100000}]
and thanks in advance.
 
You will have a lot less trouble trying to write Mathematica if you understand how to use and not use the _ in your code.

This might help.
http://reference.wolfram.com/mathematica/tutorial/DefiningFunctions.html

This
Plot[tmp[0, 3.7, w], {w, 1000, 100000}]
shows how your function behaves.

But this
FindRoot[tmp[0, 3.7, w] == 0, {w, 95000, 90000, 100000}]
fails for some reason I haven't been able to isolate in your code.

This
Plot[tmp[0, 3.7, w], {w, 90000, 95000}]
can give you a good estimate.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 9 ·
Replies
9
Views
3K