﻿ ]> Exercises

# Exercises

Use the `clear` command before starting your work on a new problem to ensure that any existing variable name assignments don't interfere with your work on the new problem.

1. Plot the function $y={3.5}^{-0.5x}\mathrm{cos}\left(6x\right)$ over the domain -2 to 4.

```     x = -2 : 0.2 : 4 ;
y = 3.5 .^(-0.5.*x) .* cos(6*x) ;
plot( x, y )```

Notice that element-wise operators must be used for the power and multiplication operations. But, why didn't we have to use the element-wise multiplication operator for the `6*x` operation? Review the notes on Operators if you don't know.

2. Plot the polynomial `f(x) = 4x3 + -5x2 + 3x - 7` over the domain `x=-2..3`.

```    x = -2 : 0.1 : 3
plot ( x, 4.*x.^3-5.*x.^2+3.*x-7 )```

Notice that you can enter the function inside the plot command. Though, we recommend that you define a function for a complex polynomials like this. The next lesson shows you how.

3. Plot the finite Fourier series given by the sum: $y\left(x\right)=\sum _{n=1}^{1000}\frac{\mathrm{sin}\left(nx\right)}{n}$ on the interval `x=(0,8π)`. The plot you should see is called the 'sawtooth function'.

Here's an algorithm (set of steps) for solving this problem without iteration (loops):

1. Create a vector for all `n` values in the sum.
2. Create a different vector for a bunch of `x` values in the range.
3. Use the `sum` command.
4. Create the `y` vector
5. Plot the results.

Note: There will be other ways to solve this problem when you learn about iteration in programming.

```     clear;
figure;
n = 1:1000 ;
x = (0:0.1:8)*pi;

SinNX = sin(n'*x);
SinNXOverN = diag(1./n) * SinNX;

SawTooth = sum( SinNXOverN );

plot( x, SawTooth );
axis([x(1) x(length(x)) -pi/2 pi/2])
title('Saw Tooth') ```