mathin
(* This file of Mathematica commands can be executed by Mathematica *)
(* Lines that begin and end like this one are ignored by Mathematica *)
(* For best results put this file in your home directory *)
(* Start Mathimatica from Unix by typing "math" *)
(* When you get a Mathimatica prompt type: "<<mathin *)
(* I've added Mathimatica "Print commands because Mathematica only *)
(* the results of the last command in a file like this. They also *)
(* make results look a little better for a situation like this. *)
(* However, you would normally not use them in an interactive session *)
(* Try typing variations on any other command you see here *)
(* *)
Print [" Find Solutions to the Equation: a x^2 + b x + c = 0"]
(* *)
(* In the line below "eqn1 = " just gives a name to the equation that *)
(* will abbreiviate later commands *)
(* *)
eqn1= a*x^2+b*x+c==0.
(* *)
(* Now generate a solution and name it soln1 *)
(* *)
soln1= Solve[eqn1,x]
(* *)
(* In an interactive session "Solve[a*x^2+b*x+c==0,x]" would be fine *)
(* Print to display the equation and solution *)
(* *)
Print ["For the equation ", eqn1]
Print ["The solutions are:",soln1]
(* *)
(* I can now go back and set values for a, b, and c and solve again *)
(* *)
a = 1
b = -1
c = -1
(* *)
Print ["For ", eqn1]
Print ["The solutions are:", N[soln1]]
(* *)
(* In the above print the Mathematica function N[ ], gives a decimal *)
(* answer for x. Depending on how your defaults are set, you may *)
(* see answers that aren't too helpful without using N[ ] *)
(* Solve is an algebraic solver and at some point in equation *)
(* difficulty will give up. Then you need to try NSolve or FindRoot *)
(* NSolve works for polynomial equations *)
(* *)
soln1= NSolve[eqn1,x]
Print ["For the equation ", eqn1]
Print ["The solutions are:",soln1]
(* *)
(* For more complicated equations Mathematica will apply Newton's method *)
(* Remember that this requires an initial guess, and only finds one *)
(* solution near the guess *)
(* For a first guess of x = 3, the solution is obtained as follows: *)
(* *)
soln1=FindRoot[eqn1,{x,3}]
Print ["Newton solution starting from 3 is:",soln1]
(* *)
(* Mathematica does an amazing number of things, take a look at a book *)
(* on the program for detailed information *)
(* One useful function is integration: *)
(* *)
answer1=Integrate[x^2*Sin[x],x]
Print["Indefinate integral of x**2 sin(x) is"]
Print[answer1]
(* *)
(* You can also do definate integrals *)
(* *)
answer2=Integrate[x^2*Sin[x],{x,0,Pi/2}]
Print["Integral of x**2 sin(x) from 0.0 to pi is"]
Print[answer2," = ",N[answer2]]
(* *)
(* If Mathematica refuses to use Integrate to evaluate a definate Integral *)
(* try the numerical integrator NIntegrate *)
(* *)
answer2=NIntegrate[x^2*Sin[x],{x,0,Pi/2}]
Print["Numerical Integral of x**2 sin(x) from 0.0 to pi is"]
Print[answer2]
(* *)
(* Another useful operation is symbolic differentiation *)
(* *)
answer3=D[x^2*Sin[x],x]
Print["Derivative of x**2 sin(x) with respect to x is"]
Print[answer3]
(* *)
(* Check you course notes for more mathematica commands and play with them *)