MATH/CMSC 206 - Introduction to Matlab

Announcements Syllabus Tutorial Projects Submitting

Answers to Self-Test

1. There are many ways one could do this. One example is as follows:

if (n>=4)
disp(['The derivative of ',char(x^n),' is ',char(diff(x^n))])
else
disp(['The derivative of ',char(sin(n*x)),' is ',char(diff(sin(n*x)))])
end

2. Here's one way:

if (sin(x) > 0)
disp(['Positive and so ',char(cos(x))])
elseif (sin(x) < 0)
disp(['Negative and so ',char(tan(x))])
else
disp(['Zero and so ',char(x)])
end

3. This is a straightforward implementation of the for loop. Our example is shown in action!

syms x;
for n=[2:6]
disp(['The 2nd derivative of ',char(sin(n*x)),' is ',char(diff(sin(n*x),2))])
end
The 2nd derivative of sin(2*x) is (-4)*sin(2*x)
The 2nd derivative of sin(3*x) is (-9)*sin(3*x)
The 2nd derivative of sin(4*x) is (-16)*sin(4*x)
The 2nd derivative of sin(5*x) is (-25)*sin(5*x)
The 2nd derivative of sin(6*x) is (-36)*sin(6*x)

4. Just change 1/100 to 1/1000.

5. The following will do the job. The subtle change is that we must change < to > in the fifth line. Do you see why? Here is both the code and its output.

Left=0;
Right=2;
while (Right-Left > 1/32)
Middle=(Left+Right)/2;
if (exp(Middle)-2 > 0)
Right=Middle;
else
Left=Middle;
end;
end;
disp(['There is a root between ',num2str(Left),' and ',num2str(Right)])
There is a root between 0.6875 and 0.71875