(A) Substitution in expressions.
> restart;
> a := t + ln(t) - sin(t);
> subs(t=5, a);
> b: = subs(t=3, a);
> evalf(b);
> subs(x=4.17, x1.8 -
x0.4 + tan(x));
> evalf(%);
> c:= evalf( subs(x=1.23, y=9.41, x^2 -
y^2 +x*y - 1) );
Exercise: Define g(x) = x sin(1/x). Find the slope of the line joining the points (1, g(1)) and (3, g(3)).
(B) Solving equations.
> restart;
> y := x^4 + 2.5*x^3 - 9.13*x^2 - 13.633*x
+ 21.25;
> plot( y, x=-4..4); # Guess
the number of roots of this polynomial
> fsolve( y = 0);
> fsolve (y = 0, x=1..3); #
Here we search for solutions over the interval 1 < x <
3.
> g := t^5 - 5.3*t^4 + 9.9*t^3 - 7.9*t^2
+ 2.3*t;
> plot( g, t = -2..5);
> fsolve (g=0);
> fsolve (g=0, t, avoid={t=0, t=1} );
# Here we exclude the two solutions t=0 and t=1.
Sometimes fsolve yields only one of the solutions that we desire. With a little bit of effort, we can find other solutions.
> restart;
> f := (x^4 - x^3 + x^2 - 13)*cos(x);
> plot(f, x = -10..10);
> plot(f, x = -2..2);
> plot(f, x = -1.4..1.6); #
Try to guess how many roots the function f has.
> fsolve(f = 0); #
Is this a surprise?
> fsolve(f = 0, x = -10..-7);
> fsolve(f = 0, x = -6..-4);
> fsolve(f = 0, x= 1..2);
> fsolve(f = 0, x= 1.4..1.6);
Can you find other roots?
(C) Graphing.
> restart;
> h := x*cos(1/x);
> plot( h, x=0.1 .. 1);
> plot( h, x=0.1 .. 1, color = BLUE);
> plot( h, x=0.1 .. 1, color = GREEN, thickness
= 3);
> plot([sqrt(x), 3*log(x)], x=0..300, title
= `The Square Root and Log Functions`);
> # Note that this single quotation mark
(often called the backquote)
is located on the far left key just under Esc.
> plot( x^3 - 5, x=0..3, axes = FRAME);
> plot( x^3 - 5, x=0..3, axes = BOXED);
> plot( x^3 - 5, x=0..3, axes = NORMAL);
We can even specify individual colors for multiple graphs:
> plot ( [sqrt(x), log(x), x^0.8], x = 0.2..1.5, color = [GREEN, VIOLET, BLUE], x=0..2);
Plotting several graphs on the same set of axes requires use of the display command if one of the graphs is that of a parameterized curve. The display command requires the use of the plots library, invoked by with(plots).
> with(plots): # Note the use of the
colon to suppress any output.
> p1 := plot (sin(x), x=0..2*Pi, color
= GREEN):
> p2 := plot (sin(2*x), x=0..2*Pi, color
= BLACK):
> p3 := plot ([t, 0.5*t^0.4, t=0..3], color
= BLUE): # This is the graph of a parameterized curve.
> display (p1, p2, p3);
(D) Controlling the number of digits.
> Digits := 5;
> evalf(exp(5);
> evalf(sin(Pi/5));
> Digits := 15;
> evalf(tan(Pi/19);
> Digits := 7;
> fsolve (sin(x)*exp(x) =1);