Excercise 1

Basic arithmetic

1. Calculate 10 sin(10)
2. Divide the result by 4
3. Add 10 to the result
4. Calculate the square of the result
5. Add together the two previous results.

Solution

You can include comments inside Mathematica code by enclosing them between (* and *).

10  Sin[10]

%/4

% + 10

%*%

% + %%

The function N gives the numerical value of an expression

% // N

Substitutions

Direct substitution

1. Set the variable x equal to 10 and y equal to 5 using direct substitution
2. Set z equal to 2x+y using direct substitution. Check the value of z.
3. Set x equal to 8
5. What's the value of z now? Try it.

Clear x, y and z

Solution

x = 10;  y = 5;

z = 2  x + y

x = 8;

z

Clear[x, y, z]

Delayed substitution

1. Set x and y equal to 3 and 4.
2. Using delayed substitution, set z equal to x*y
3. Check the value of z
4. Set x equal to 5
5. What's the value of z now? Try it.
6. Clear y and check what is z.

Clear x, y and z

Solution

x = 3;  y = 4;

z := x*y

Note that delayed substitution produces no output.

z

x = 5;

z

Clear[y]

    z

Clear[x, y, z]

Extra excercises

1. Think what would a:=a+1 give.
2. Try it. Try to get the value of a.
3. Interprete the result
4. What does a=a+1 give?

Clear a.

a := a + 1

    a

a = a + 1

This kind of substitution leads to an infinite sequence
	a=a+1=(a+1)+1=((a+1)+1)+1=...
Mathematica does the substitution 256 times, which is set as the maximum recursion depth and then prints out an error message. Using delayed and direct substitutions lead to essentially the same result. With delayed substitution, the error is generated when you try to evaluate the expression.

Remember to clear a after this!

Clear[a]

Truth values

1. Evaluate 10 != 11
2. Evaluate 0==0.
3. Evaluate 1<=3.
4. Evaluate 10==a.
5. Evaluate -10 < 10.

Solution

10 ≠ 11

0 == 0

1 ≤ 3

10 == a

-10 < 10

Bonus excercises

Mathematica can check the types of variables and numbers. Evaluate the following:
1. NumberQ[1.24]
2. EvenQ[4]
3. OddQ[6]
4. PrimeQ[7]

Solution

NumberQ[1.24]

EvenQ[4]

OddQ[6]

PrimeQ[7]

Lists

Elementarities

1. Construct the list {1,2,3,5,7,11} and set it to the variable x.
2. Add together the first and last element of x.
3. Multiply the second element with the third element and add the fourth element to the product.

Clear x.

Solution

x = {1, 2, 3, 5, 7, 
        11}

x[[1]] + 
      x[[6]]

Another way to do the same thing is with the functions First and Last

First[x] + Last[x]

x[[2]]*x[[3]] + 
      x[[4]]

Clear[x]

More elementarities

1. Construct the list {a,b,c} and set it to variable x.
2. Construct the list {1,2,3} and set it to y.
3. What would x+y give? Try it.

Clear x and y

Solution

x = {a, b, c}

y = {1, 2, 3}

x + y

Clear[x, y]

Two-dimensional lists

1. Set A equal to the list {{1,2,3}, {4,5,6}}.
2. Extract the second element of A.
3. Extract the third element of the first element of A.
4. Extract the first element of the second element of A.
5. Add together the two previous results.

Clear A.

Solution

The function MatrixForm will print the list in two-dimensional form.

A = {{1, 2, 3}, {4, 5, 
            6}};

    MatrixForm[A]

A[[2]]

A[[1, 3]]

A[[2, 1]]

% + %%

Clear[A]

Extra excercises

1. Construct the matrix (({{a, b}, {c, d}, {e, f}})) and set it to A.
2. Extract the last element of the first row of A.
3. How would you extract the element e from A?

Clear A.

Solution

A = {{a, b}, {c, d}, {e, f}};  
    MatrixForm[A]

There are again two ways to do excercise 2

A[[1, 2]]

    Last[First[A]]

A[[3, 1]]

Rules, equations, solving equations

First

1. Calculate (x^2) + x + 1with x equal to 10
2. Calculate x + y with x and y equal to 1 and 2
3. Calculate ((a + 1)/(b + 2))with a and b equal to 3 and 4
4. Set x equal to 10 using a rule and a substitution operator.

Solution

x*x + x + 1 /. 
      x -> 10

x + y /. {x -> 1, 
        y -> 2}

(a + 1)/(b + 
            2) /. {a -> 3, b -> 4}

x = 
      x /. x -> 10

This is of course ridiculous.

Second

1. Solve the equation (x^2) + p x + q = 0, and set the result to the variable e.
2. Extract the second element of the solution list.
3. Using the above rule, calculate x + p.
4. Calculate the above with p = 1and q = 2.
5. Check the numerical value of the above using the function N[...].

Solution

e = 
      Solve[x*x + p x + q == 0, x]

%[[2]]

x + p /. %

% /. {p -> 1, 
        q -> 2}

N[%]

Third

1. Set the variable Y equal to the equation (x^3) + p (x^2) + q x + r = 0.
2. Solve equation Y.
3. Check the solution by substituting it into the equation and simplifying the resulting expression

Solution

Y = 
      x*x*x + p*x*x + q*x + r == 0

Solve[Y, x]

Y /. %

    Simplify[%]

Substituting a rule into an equation yields a logical expression, which can be simplified into another logical expression, which in this case is simply the expression True.

Fourth

1. Set the variable y equal to ((6 + 11 x + 6 (x^2) + (x^3))/(-x + (x^3)))
2. Apply the functions Expand, Factor, Together, Apart, Cancel, Simplify, Numerator ja Denominator on y.
3. Solve the equation y = 0by taking the numerator of y and finding its zeros.
4. Check the solution by substituting it into the equation. What do you see? How could you fix the situation?

Solution

y = (6 + 11  x + 6  x*x + 
            x*x*x)/(-x + x*x*x)

Expand[y]

Factor[y]

Together[y]

Apart[y]

Cancel[y]

Simplify[y]

Numerator[y]

Denominator[y]

Solve[Numerator[y] == 0, 
      x]

y == 0 /. %

The solution x=-1 is a zero of the denominator of y. This can be seen from the various simplified versions of y, where common factors have been cancelled out. Since x=-1 is a zero of the denominator, y is not defined there. We could just drop the third solution alltogether. Another way to proceed is to solve instead for the zeros of the numerator of one of the simplified versions of y, for example Factor[y]. These are of course trivial.

Solve[Numerator[Factor[y]] == 0, x]

Clear[y]

Extra excercise

1. Solve the set of equations {({{((a x)/(Sqrt[y])) = 4}, {b x + a y = 3}})for x and y. Use the function Sqrt[] for the square root.
2. Simplify the above result
3. Set above a and b equal to 1 and 2.
4. Calculate the numerical value of the above result using the function N.

Solution

Solve[{a*x/Sqrt[y] == 4, 
        b*x + a*y == 3}, {x, y}]

Simplify[%]

% /. {a -> 1, 
        b -> 2}

% // N