MATH/CMSC 206 - Introduction to Matlab

Announcements Syllabus Tutorial Projects Submitting

Answers to Self-Test

1. We'll do this cleanly - first define the variable as symbolic, then the function, then take the derivative:

syms x;
f=sin(x^2);
diff(f,x)
 
ans =
 
2*x*cos(x^2)
 

Note here that if it's clear what the variable is we can actually get away with not passing it to the diff command:

syms x;
f=sin(x^2);
diff(f)
 
ans =
 
2*x*cos(x^2)
 

2. Again nice and clean:

syms x;
f=x^2*sin(x);
int(f,x,0,pi/2)
 
ans =
 
pi - 2
 

3. Remember from calculus the slope of the tangent line is the derivative so we need to find the derivative and plug in x=2.

syms x;
f=x*exp(x)+x^3;
subs(diff(f),x,2)
ans =

   34.1672

4. Remember inline functions are defined with the inline command:

f=inline('x^2-3*x','x');
f(-2)
ans =

    10

5. Function handles are defined using the crazy @ symbol and zeros can be found with the fzero command.

h=@(x) x^2-7;
fzero(h,2)
ans =

    2.6458

It's worth noting that finding where x^2-7=0 is the same as finding the square root of 7, this case the positive square root, the one near 2.