How to call a built function in mathematica

In summary, the conversation was about a request for help in using Mathematica's programming language. The person had written a code that worked as a function but was having trouble calling it in another function. They were advised to break down their code into simpler steps and try it on simpler examples to find the error. The conversation also included a discussion about using the _ symbol and a link to a tutorial on defining functions. The code in question was shown and the person was able to use it to plot a graph, but had trouble using it in the FindRoot function. The conversation ended with a suggestion to use the graph to estimate the value needed for the FindRoot function.
  • #1
Reem Hashem
23
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

  • Untitled-1.nb
    35.5 KB · Views: 378
Physics news on Phys.org
  • #2
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.
 
  • #3
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
 
  • #4
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.
 
  • #5
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.
 

1. How do I call a built-in function in Mathematica?

To call a built-in function in Mathematica, you simply need to type the name of the function followed by the arguments within square brackets. For example, if you want to use the "Sqrt" function to find the square root of 9, you would type "Sqrt[9]" and press enter.

2. Can I create my own functions in Mathematica?

Yes, you can create your own functions in Mathematica using the "Function" command. This allows you to define a function with your desired name, arguments, and code. You can also save your custom functions for future use.

3. How do I know which arguments to use for a built-in function?

You can find the arguments needed for a built-in function by looking at its documentation. In Mathematica, you can access the documentation for a function by placing your cursor on the function name and pressing "F1". This will bring up a window with information on the function, including its arguments.

4. Is there a way to call multiple functions at once in Mathematica?

Yes, you can call multiple functions at once in Mathematica by using the "CompoundExpression" command, which is denoted by a semicolon. This allows you to execute multiple commands in a single line of code, making your code more efficient.

5. Can I call a function only on certain elements in a list or array?

Yes, you can use the "Map" function in Mathematica to apply a function to each element in a list or array. This is useful if you only want to perform the function on specific elements rather than the entire list. You can also use the "Select" function to filter out elements that do not meet a certain criteria before applying the function.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
399
  • Differential Equations
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • Programming and Computer Science
Replies
1
Views
681
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
248
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
Back
Top