© Agus Kurniawan 2019
Agus KurniawanRaspbian OS Programming with the Raspberry Pihttps://doi.org/10.1007/978-1-4842-4212-4_4

4. Computational Mathematics with the Wolfram Language and Mathematica

Agus Kurniawan1 
(1)
Depok, Indonesia
 

The Wolfram Language and Mathematica are exclusive tools and libraries of the Raspbian OS on the Raspberry Pi board. In this chapter, we explore the Wolfram Language and Mathematica from a programming view.

The following is a list of topics covered in this chapter:
  • Understand the Wolfram Language and Mathematica

  • Set up Wolfram and Mathematica

  • Develop a Hello World program

  • Learn basic programming for Wolfram and Mathematica

  • Learn computational mathematics with Wolfram and Mathematica

4.1 Introducing Wolfram Language and Mathematica

Wolfram Language and Mathematica are available for the Raspberry Pi board and come bundled with the Raspbian operating system. Programs can be run from a Pi command line or as a background process, as well as through a notebook interface on the Pi or on a remote computer. On the Pi, the Wolfram Language supports direct programmatic access to standard Pi ports and devices. For further information about this project, you can visit http://www.wolfram.com/raspberry-pi/ .

After you have deployed Raspbian on Raspberry Pi, you can run Raspbian in GUI mode and then you should see the Wolfram and Mathematica icons. You can see these icons in Figure 4-1.
../images/474433_1_En_4_Chapter/474433_1_En_4_Fig1_HTML.jpg
Figure 4-1

The Wolfram and Mathematica icons on the Raspbian bar

You also find these application icons in the main menu, as shown in Figure 4-2.
../images/474433_1_En_4_Chapter/474433_1_En_4_Fig2_HTML.jpg
Figure 4-2

The Wolfram and Mathematica menu options on the Raspbian main menu

The Wolfram application has a Terminal form to run programs. If you execute the Wolfram application, you should see the Terminal application, as shown in Figure 4-3.
../images/474433_1_En_4_Chapter/474433_1_En_4_Fig3_HTML.jpg
Figure 4-3

Running the Wolfram application from the Terminal

Otherwise, you can execute the Mathematica application. Then, you get the form shown in Figure 4-4.
../images/474433_1_En_4_Chapter/474433_1_En_4_Fig4_HTML.jpg
Figure 4-4

Running the Mathematica application

Next, we learn how these tools work with computation mathematics.

4.2 Setting Up Wolfram and Mathematica

Technically, we don’t discuss setting up Wolfram and Mathematica in any more detail. If you download and deploy the latest Raspbian on the Raspberry Pi boards, you will get free licenses for Wolfram and Mathematica.

In the next section, we create a simple program on Wolfram and Mathematica.

4.3 Developing a Hello World Program

To get experience creating programs using Wolfram and Mathematica, we will build a simple program, using a simple mathematics operation. For demo purposes, you can click the Wolfram Mathematica icon. Then, you get the Wolfram Mathematica Editor, shown in Figure 4-5.

Now type these scripts:
a = 3
b = 5
c = a * b
../images/474433_1_En_4_Chapter/474433_1_En_4_Fig5_HTML.jpg
Figure 4-5

Running programs on Mathematica

To run these scripts, press Shift+Enter (press the Shift and Enter keys together) and you’ll obtain the resulting output. Sample output can be seen in Figure 4-5.

You also can run the same scripts in the Wolfram Terminal. You can type the script line-by-line. Then, you will get response directly. Sample program output from the Wolfram Terminal can be seen in Figure 4-6.
../images/474433_1_En_4_Chapter/474433_1_En_4_Fig6_HTML.jpg
Figure 4-6

Running programs from the Wolfram Terminal

4.4 Basic Programming

In this section, you learn how to write programs for Wolfram and the Mathematica language. You learn the essential programming language steps from Wolfram and Mathematica.

You can follow the guidelines in the next sections.

4.4.1 Data Types and Declaring Variables

You can declare a variable and assign a value using the = syntax. If you don’t want to assign a value to a variable, you can set . as the value. You also can use := to delay assignment.

On a notebook from the Mathematica Editor, you can type these scripts:
a =.
b = 3
c := 5
str = "hello world"
Run these scripts by pressing the Shift+Enter keys. You can see the program output in Figure 4-7.
../images/474433_1_En_4_Chapter/474433_1_En_4_Fig7_HTML.jpg
Figure 4-7

Declaring variables

4.4.2 Arithmetic Operators

Mathematica has arithmetic operators that you can use to manipulate numbers. The following is a list of its arithmetic operators:
  • + addition

  • - subtraction

  • * multiplication

  • / division

  • ^ exponentiation

For demo purposes, write these scripts.
m = 8
n = 5
p = m + n
p = m - n
p = m * n
p = m / n
p = m^2
The sample program output is shown in Figure 4-8.
../images/474433_1_En_4_Chapter/474433_1_En_4_Fig8_HTML.jpg
Figure 4-8

Program output for arithmetic operators

4.4.3 Relational and Logical Operators

We can perform logical operations in Mathematica as well. The following is a list of logical operators:
  • < less than

  • > greater than

  • <= less than or equal

  • >= greater than or equal

  • == equal to

  • != not equal to

  • || or

  • ! not

  • && and

  • xor[a,b] exclusive or

For further information, I recommend you read the Wolfram and Mathematica documentation, https://reference.wolfram.com/language/tutorial/RelationalAndLogicalOperators.html .

For demo purposes, we examine logical operators. Write the following scripts.
m = 10
n = 5
m < n
m <= n
m > n
m >= n
m == n
m != n
(m < n) && (m > 10)
(m <= 2) || (n > 2)
You can see this program output in Figure 4-9.
../images/474433_1_En_4_Chapter/474433_1_En_4_Fig9_HTML.jpg
Figure 4-9

Program output for logical operations

4.4.4 Conditional Statements

Sometimes we want to manipulate data with a conditional. In Mathematica, we can use the if and switch statements. We explore these statements in the next sections.

4.4.4.1 if

We can write a Mathematica script for conditional statements, using the If[] statement . The following is general statement of if[].
If[statement,do if true, do if false]
Let’s write this example:
x = 3;
y = 0;
If[x > 1, y = Sqrt[x], y = x^2];
Print[y]
m := If[x > 5, 1, 0];
Print[m]
You can type this program into Mathematica and you will get the program output shown in Figure 4-10.
../images/474433_1_En_4_Chapter/474433_1_En_4_Fig10_HTML.jpg
Figure 4-10

Program output from an if application

4.4.4.2 switch

You can select one of several options in Mathematica. You can use the Switch[] statement. Switch[] can be defined as follows:
Switch[statement, case 1, do case 1 ...., case n,do case n, _ , do default case]
A sample Switch[] script can be written as follows:
k = 2;
n = 0;
Switch[k, 1, n = k + 10, 2, n = k^2 + 3, _, n = -1];
Print[n]
k = 5;
n := Switch[k, 1, k + 10, 2, k^2 + 3, _, -1];
Print[n]

Note

Print[] is used to print a message on the Terminal.

Run the program. You will get the program output that is shown in Figure 4-11.
../images/474433_1_En_4_Chapter/474433_1_En_4_Fig11_HTML.jpg
Figure 4-11

Program output sample for Switch[]

4.4.5 Looping

If you perform continuous tasks, it’s wise to use a looping scenario to get those tasks done. In Mathematica, you can perform looping using the following statements:
  • do statements

  • for statements

  • while statements

Each type of looping statement will be reviewed in the next sections.

4.4.5.1 Do

The first looping statement is the Do[] statement . The Do[] statement can be defined as follows:
Do[do_something, {index}]

index is the amount of looping.

For instance, if you want to print “Hello Mathematica” five times, you would write this script.
Do[Print["Hello Mathematica"], {5}]
Run the program in Mathematica. If you do so, you’ll get the response shown in Figure 4-12.
../images/474433_1_En_4_Chapter/474433_1_En_4_Fig12_HTML.jpg
Figure 4-12

Program output for a do statement

You also can include an index number in the looping statement. You can write this script for example:
Do[Print["counter ", i], {i, 5}]
After it’s executed, you’ll see the output shown in Figure 4-13.
../images/474433_1_En_4_Chapter/474433_1_En_4_Fig13_HTML.jpg
Figure 4-13

Displaying an index on a looping program

4.4.5.2 For

If you have experience with C/C++, you can use a for statement in the same way. The For[] statement in Mathematica is defined as follows:
For[initial,conditional, increment/decrement, do_something]
For testing purposes, you can print a message five times. Write this script.
For[i = 0, i < 5, i++, Print["index ", i]]
Run the script and you’ll see the program output shown in Figure 4-14.
../images/474433_1_En_4_Chapter/474433_1_En_4_Fig14_HTML.jpg
Figure 4-14

A looping program with For[]

4.4.5.3 While

The last looping statement that we can use in Mathematica is While[] . This statement can be defined as follows:
While[conditional, do_something]
For demo purposes, write this script:
j = 0;
While[j < 5, Print["j=", j]; j++;]
This will print value 0 through value 4. You can see the program output in Figure 4-15.
../images/474433_1_En_4_Chapter/474433_1_En_4_Fig15_HTML.jpg
Figure 4-15

Looping program written with While[]

4.4.5.4 Break and Continue

Break[] is used to exit the nearest enclosing loop. Continue[] is used to go to the next step in the current loop. Try writing these scripts for demo purposes:
For[i = 0, i < 10, i++,
 Print[i];
 If[i == 2, Continue[]];
 If[i == 5, Break[]]
]
The program will stop a moment when the value of i is 2. The program also stops on value i = 5. You can see the program output in Figure 4-16.
../images/474433_1_En_4_Chapter/474433_1_En_4_Fig16_HTML.jpg
Figure 4-16

Program demo using Break[] and Continue[]

4.4.6 Adding Comments

To add comments to your code, which is a very good idea, you can use the (* *) characters. You should use comments in your scripts, which the application will not run, to explain what certain lines of code and scripts are meant to do. Figure 4-17 shows comments added to scripts.
../images/474433_1_En_4_Chapter/474433_1_En_4_Fig17_HTML.jpg
Figure 4-17

Adding comments to scripts

4.4.7 Functions

If you have scripts that are called continuously, consider using functions to avoid redundant scripts. You can wrap them into a function. A function in Mathematica can be defined as follows:
function[params_] := do_something
For testing purposes, we define a simple Mathematics function as follows:
Clear[f, x, y];
f[x_] := (x - 2) * Sqrt[x];
Print[f[15]];
Print[f[8]];
You should get the program output shown in Figure 4-18 when you execute these scripts.
../images/474433_1_En_4_Chapter/474433_1_En_4_Fig18_HTML.jpg
Figure 4-18

A function in Mathematica

In a function, you can define parameters as input. You can also add two parameters or more. For instance, you can write these scripts.
Clear[f, x, y];
f[x_, y_] := x^2 + y;
Print[f[5, 8]];
Print[f[2, 3]];
The program output of these scripts can be seen in Figure 4-19.
../images/474433_1_En_4_Chapter/474433_1_En_4_Fig19_HTML.jpg
Figure 4-19

A function with parameters

You can implement a recursive function in Mathematica as well. For instance, you can write these scripts:
(* recursive functions *)
ClearAll[h, n];
h[0] = 10;
h[n_] := 0.58 * h[n - 1];
Print[h[10]];
Now you can run this program. Figure 4-20 shows the program output sample.
../images/474433_1_En_4_Chapter/474433_1_En_4_Fig20_HTML.jpg
Figure 4-20

A sample of a recursive function

4.5 Computational Mathematics

This chapter explains how to work with computational mathematics using Mathematica. We will discuss several mathematics problems as follows:
  • Calculus

  • Matrix

  • Quadratic equations

  • Linear equations

Let’s go.

4.5.1 Calculus

There are many topics in calculus. In this section, we focus on plotting an equation, defining limits, performing differentiation, performing integration, and summing.

We explore these topics in the next sections.

4.5.1.1 Plot

We can use Plot[] to display a graph. You can read more about this function at https://reference.wolfram.com/language/ref/Plot.html .

For demo purposes, we create two equations as follows.
$$ {displaystyle egin{array}{l}kern0.75em {f}_1=cos kern0.125em x\ {}{f}_2={x}^2-2sin kern0.125em xend{array}} $$
On Mathematica, we can implement this as follows.
Plot[Cos[x], {x, 0, 3 Pi}]
Plot[x^2 - 2 Sin[x], {x, 0, 3 Pi}]

You can see the program output in Figure 4-21.

Furthermore, try some more examples. Plot the following equations.
../images/474433_1_En_4_Chapter/474433_1_En_4_Fig21_HTML.jpg
Figure 4-21

Plotting math equations

$$ {displaystyle egin{array}{l}kern0.125em {f}_1=sin kern0.125em x\ {}{f}_2=cos kern0.125em x\ {}{f}_3=0.1x-1\ {}{f}_4=0.5sin kern0.125em xend{array}} $$
Those equations can be implemented in Mathematica as follows.
Plot[{Sin[x], Cos[x], 0.1 x - 1, 0.5 Sin[x]}, {x, 0, 6 Pi},
 PlotLegends -> "Expressions"]
The program output from those scripts can be seen in Figure 4-22.
../images/474433_1_En_4_Chapter/474433_1_En_4_Fig22_HTML.jpg
Figure 4-22

Plotting the sin and cos equations

4.5.1.2 Limits

We can implement limits in Mathematica using the Limit[] syntax . For more information about this statement, check it out at https://reference.wolfram.com/language/ref/Limit.html .

For testing purposes, we want to display our limit equation as follows.
$$ underset{x	o infty }{lim}frac{6x+1}{2x+5} $$
We can implement this equation using Mathematica as follows.
HoldForm[Limit[(6 x + 1)/(2 x + 5), x -> Infinity]]
Figure 4-23 shows the program output from this script.
../images/474433_1_En_4_Chapter/474433_1_En_4_Fig23_HTML.jpg
Figure 4-23

Displaying limits

Let’s practice some more with this statement. See the following limit problems. We want to calculate these limit equations.
$$ {displaystyle egin{array}{c}underset{x	o infty }{lim}frac{6x+1}{2x+5}\ {}underset{x	o infty }{lim}frac{x^2}{1-{x}^2}\ {}underset{x	o 0}{lim}frac{sqrt{x+25}-5}{x}\ {}underset{x	o 2}{lim}frac{x^2-7x+10}{x-2}end{array}} $$
You can implement these equations in Mathematica as follows.
(* compute limit *)
Limit[(6 x + 1)/(2 x + 5), x -> Infinity]
Limit[x^2/(1 - x^2), x -> Infinity]
Limit[(Sqrt[x + 25] - 5)/x, x -> 0]
Limit[(x^2 - 7 x + 10)/(x - 2), x -> 2]
The program output of these scripts can be found in Figure 4-24.
../images/474433_1_En_4_Chapter/474433_1_En_4_Fig24_HTML.jpg
Figure 4-24

Calculating more math limits

4.5.1.3 Differentiation

In this section, we use differentiation. We can use the D[] syntax to implement differentiation. Read more about it at https://reference.wolfram.com/language/ref/D.html .

For testing purposes, consider the following problem.
$$ {displaystyle egin{array}{l}kern1.25em f(x)={x}^2\ {}{f}^{prime }(x)=frac{df}{dx}=dots ??end{array}} $$
The following is the implementation of this problem in Mathematica.
D[x^2, x]
Run this script. You can see the program output in Figure 4-25.
../images/474433_1_En_4_Chapter/474433_1_En_4_Fig25_HTML.jpg
Figure 4-25

Calculating differentiation

Let’s try some more examples. Consider the following differentiation problems.
$$ {displaystyle egin{array}{l}f(x)=frac{x^3}{4}+frac{1}{x^2}\ {}f(x)=frac{1}{1+frac{1}{x}}\ {}f(x)={x}^2{e}^xcos kern0.125em 2x\ {}f(x)=log x{e}^xend{array}} $$
A simple solution in Mathematica follows.
(* compute *)
D[(x^3/4) + (1/x^2), x]
D[1/(1 + 1/x), x]
D[x^2 e^x Cos[2 x], x]
D[Log[x e^x], x]
The program output is shown in Figure 4-26.
../images/474433_1_En_4_Chapter/474433_1_En_4_Fig26_HTML.jpg
Figure 4-26

Solving differentiation problems

4.5.1.4 Integration

In this section, we perform two integration models—indefinite integrals and definite integrals. We explore both of these models.

4.5.1.4.1 Indefinite Integrals

First, we explore the indefinite integral. You can implement indefinite integrals using the Integrate[] statement. For more information about this statement, visit https://reference.wolfram.com/language/ref/Integrate.html .

For instance, consider the following math problems.
$$ {displaystyle egin{array}{l}kern2.25em f(x)=2x\ {}int f(x)kern0.125em dx=int 2xkern0.125em dx=dots ??end{array}} $$
You can implement this in Mathematica, using Integrate[] as follows.
Integrate[2 x, x]
The program output from Mathematica can be seen in Figure 4-27.
../images/474433_1_En_4_Chapter/474433_1_En_4_Fig27_HTML.jpg
Figure 4-27

Calculating a math integral

Let’s try some more examples. The following are more indefinite integral problems.
$$ {displaystyle egin{array}{c}int frac{1}{4+3x} dx\ {}int sin xkern0.125em cos kern0.125em x dx\ {}int {left(sin kern0.125em 3x
ight)}^2+{left(cos kern0.125em 3x
ight)}^2 dx\ {}int {left(4xhbox{-} 2
ight)}^6 dxend{array}} $$
You can solve those problems in Mathematica. You can write these scripts:
Integrate[1/(4 + 3 x), x]
Integrate[Sin[x] Cos[x], x]
Integrate[Sin[3 x]^2 + Cos[3 x]^2, x]
Integrate[(4 x - 2)^6, x]
You can see the solution in Figure 4-28.
../images/474433_1_En_4_Chapter/474433_1_En_4_Fig28_HTML.jpg
Figure 4-28

Solving indefinite integral problems

4.5.1.4.2 Definite Integrals
To implement a definite integral, you set the starting and ending values of the Integrate[] statement. Consider the following definite integral problems, for example.
$$ {displaystyle egin{array}{c}underset{0}{overset{5}{int }}{x}^2kern0.125em dx\ {}underset{0}{overset{1}{int }}x{e}^{x^2}kern0.125em dx\ {}underset{0}{overset{frac{pi }{2}}{int }}cos kern0.125em x {left(sin x
ight)}^5kern0.125em dx\ {}egin{array}{l}underset{0}{overset{frac{pi }{2}}{int }}xcos kern0.125em x dx\ {}underset{0}{overset{2}{int }}frac{1}{2x+5} dxend{array}end{array}} $$
You can solve those problems in Mathematica as follows.
(* definite integral *)
Integrate[x ^2, {x, 0, 5}]
Integrate[x e^(x^2), {x, 0, 1}]
Integrate[Cos[x] Sin[x]^5, {x, 0, Pi/2}]
Integrate[x Cos[x], {x, 0, Pi/2}]
Integrate[1/(2 x + 5), {x, 0, 2}]
The program output is shown in Figure 4-29.
../images/474433_1_En_4_Chapter/474433_1_En_4_Fig29_HTML.jpg
Figure 4-29

Solving definite integral problems

4.5.1.5 Summing

In mathematics, there are many math problems related to summation. You can use Sum[] to calculate sums. For further information about Sum[], you can visit https://reference.wolfram.com/language/ref/Sum.html .

For instance, we have a formula as follows.
$$ sum limits_{i=0}^{10}2i=dots ..?? $$
You can calculate that problem using Mathematica. Write scripts in Mathematica as follows.
Sum[2 i, {i, 10}]
You can see the program output in Figure 4-30.
../images/474433_1_En_4_Chapter/474433_1_En_4_Fig30_HTML.jpg
Figure 4-30

Calculating summation in Mathematica

Let’s look at some more examples. The following are summation problems.
$$ {displaystyle egin{array}{c}sum limits_{i=5}^{10}frac{2i-3}{5i}\ {}sum limits_{i=0}^n4iend{array}} $$
You can solve these problems in Mathematica. You can write these scripts.
Sum[(2 i - 3)/5 i, {i, 5, 10}]
Sum[4 i, {i, n}]
Run these scripts in Mathematica. You should get the output shown in Figure 4-31.
../images/474433_1_En_4_Chapter/474433_1_En_4_Fig31_HTML.jpg
Figure 4-31

Solving summation problems in Mathematica

4.5.2 Matrix

In Mathematica, you can display a matrix using MatrixForm[].

For instance, say you want to display this matrix.
$$ {displaystyle egin{array}{l}left(egin{array}{c}a\ {}b\ {}cend{array}
ight)\ {}left(egin{array}{ccc}1&amp; 2&amp; 3\ {}4&amp; 5&amp; 6\ {}7&amp; 8&amp; 9end{array}
ight)end{array}} $$
In Mathematica, you can formulate that matrix with the following scripts.
(* matrix *)
MatrixForm[{a, b, c}]
MatrixForm[{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}]
Run the script to see the matrix forms. The program output is shown in Figure 4-32.
../images/474433_1_En_4_Chapter/474433_1_En_4_Fig32_HTML.jpg
Figure 4-32

Displaying a matrix in Mathematica

You also can display a matrix using // MatrixForm. Write this script.
k = {{a, b, c}, {d, e, f}};
k // MatrixForm
Now we do matrix operations such as addition, subtraction, and multiplication. Consider the following cases.
$$ {displaystyle egin{array}{l}left(egin{array}{c}5\ {}6end{array}
ight)+left(egin{array}{cc}2&amp; 3\ {}4&amp; 2end{array}
ight)=left(egin{array}{cc}7&amp; 8\ {}10&amp; 8end{array}
ight)\ {}-left(egin{array}{c}5\ {}6end{array}
ight)+left(egin{array}{cc}2&amp; 3\ {}4&amp; 2end{array}
ight)=left(egin{array}{cc}-3&amp; -2\ {}-2&amp; -4end{array}
ight)\ {}left(egin{array}{cc}2&amp; 3\ {}4&amp; 2end{array}
ight)cdot left(egin{array}{c}5\ {}6end{array}
ight)=left(egin{array}{c}28\ {}32end{array}
ight)end{array}} $$
You can implement them in Mathematica scripts as follows.
(* matrix operations *)
Clear[m, n];
m = {{2, 3}, {4, 2}};
n = {5, 6};
(n // MatrixForm) + (m // MatrixForm) == (m + n // MatrixForm)
(m // MatrixForm) - (n // MatrixForm) == (m - n // MatrixForm)
(m // MatrixForm) . (n // MatrixForm) == (m . n // MatrixForm)
The program output is shown in Figure 4-33.
../images/474433_1_En_4_Chapter/474433_1_En_4_Fig33_HTML.jpg
Figure 4-33

Solving matrix operations

If you want to transpose a matrix, you can use Transpose[].

For instance, consider this matrix as an example.
$$ left(egin{array}{lll}2&amp; 3&amp; 4\ {}5&amp; 6&amp; 7end{array}
ight) $$
You can transpose this matrix with the following scripts.
(* Transpose *)
L = {{2, 3, 4}, {5, 6, 7}};
L // MatrixForm
Transpose[L] // MatrixForm
Run these scripts and you should get program output shown in Figure 4-34.
../images/474433_1_En_4_Chapter/474433_1_En_4_Fig34_HTML.jpg
Figure 4-34

Transposing a matrix

You can find determinants, inverses, and matrix ranks from a matrix as well. For instance, consider this matrix.
$$ left(egin{array}{cc}2&amp; 4\ {}7&amp; 8end{array}
ight) $$
To calculate a determinant, inverse, and rank using Mathematica, you would use the following scripts:
(* Determinant *)
A = {{2, 4}, {7, 8}};
A // MatrixForm
Det[A]
Inverse[A] // MatrixForm
MatrixRank[A]
The program output from these scripts can be seen in Figure 4-35.
../images/474433_1_En_4_Chapter/474433_1_En_4_Fig35_HTML.jpg
Figure 4-35

Calculating determinant, inverse, and matrix rank from a matrix

4.5.3 Quadratic Equations

We find values of unknown parameters in quadratic equations using Solve[]. For more information about the Solve[] statement, visit https://reference.wolfram.com/language/ref/Solve.html .

For instance, we have the quadratic equation shown here.
$$ {x}^2-3xhbox{-} 4=0 $$
Now we can find the x values using Solve[].
(* Quadratic Equations *)
Solve[x^2 - 3 x - 4 == 0, x]

Run the program and you will see that the x values are -1 and 4.

The program output is shown in Figure 4-36.
../images/474433_1_En_4_Chapter/474433_1_En_4_Fig36_HTML.jpg
Figure 4-36

Solving quadratic equations

Let’s look at some other examples. The following are quadratic equation problems. Find the x values.
$$ {displaystyle egin{array}{c}{x}^2-4=0\ {}6{x}^2+11x-35=0\ {}{x}^2-7x=0end{array}} $$
Those problems can be solved in Mathematica. You can write these scripts to solve for x.
Solve[x^2 - 4 == 0, x]
Solve[6 x^2 + 11 x - 35 == 0, x]
Solve[x^2 - 7 x == 0, x]
The program output from Mathematica can be seen in Figure 4-37.
../images/474433_1_En_4_Chapter/474433_1_En_4_Fig37_HTML.jpg
Figure 4-37

Quadratic equation solutions in Mathematica

4.5.4 Linear Equations

In linear equations, we solve to find values from a set of parameters. We can use the Solve[] statement in Mathematica to do this. For more information about this statement, visit https://reference.wolfram.com/language/ref/Solve.html .

For instance, we have three equations:
$$ {displaystyle egin{array}{l}kern0.375em x+y+z=0\ {}x+2y+3z=1\ {}kern0.375em x-y+z=2end{array}} $$
We can find the x, y, and z values in Mathematica.
Solve[{x + y + z, x + 2 y + 3 z, x - y + z} == {0, 1, 2}]
Another problem you can solve is as follows.
$$ {displaystyle egin{array}{l} 3x-y=0\ {} y+2z=1\ {}x+y+z=3end{array}} $$
The solutions from these problems in Mathematica are as follows.
Solve[{3 x - y, y + 2 z, x + y + z} == {0, 1, 3}]
Run all these scripts. You’ll see the program output in Figure 4-38.
../images/474433_1_En_4_Chapter/474433_1_En_4_Fig38_HTML.jpg
Figure 4-38

Solving linear equations using Mathematica

You can see the x, y, and z values.

Now we have another problem. The w parameter shown here is a constant value.
$$ {displaystyle egin{array}{l}2x+3y-7z+w=8\ {}4x-2y+z+2w=4\ {}5x+y-4z+3w=6end{array}} $$
The solutions are written in Mathematica, as shown in the following script.
Solve[{2 x + 3 y - 7 z + w, 4 x - 2 y + z + 2 w, 5 x + y - 4 z + 3 w} == {8, 4, 6}]
The program output can be seen in Figure 4-39.
../images/474433_1_En_4_Chapter/474433_1_En_4_Fig39_HTML.jpg
Figure 4-39

Solving linear equations using Mathematica

4.6 Summary

In this chapter, you learned about the Wolfram and Mathematica programs in Raspbian OS that run on Raspberry Pi boards. You also played math games such as computational mathematics to see how to work with Mathematica.

In the next chapter, we focus on visual programming using Scratch on the Raspbian OS and Raspberry Pi boards.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.228.40