Back to tutorial index

# Lab #2 - Solutions

## Introduction

Back to lab exercises.

>> format long

Back to the top

## Exercise 1

>> f = @(x) tanh(x/10)
f =

@(x)tanh(x/10)


>> f(2*pi)
ans =

0.556893306900211


>> f(-exp(1))
ans =

-0.265325141803780



Back to the top

## Exercise 2

>> h = @(x) (3*x^3 - 1)/(x^2 + 3)
h =

@(x)(3*x^3-1)/(x^2+3)


>> h(sqrt(3)*i)
ans =

-2.251799813685248e+15 - 3.510208517199282e+16i


>> h(nthroot(1/3,3))
ans =

-6.379217526741067e-17



Back to the top

## Exercise 3

>> s = @(t) cos(t)
s =

@(t)cos(t)


>> q  = @(t) 1/(s(t)^2 + 1)
q =

@(t)1/(s(t)^2+1)


>> q(pi)
ans =

0.500000000000000



Back to the top

## Exercise 4

>> T = @(x,y) 5/x^2 - 1 + y + 0.5*x^2/y
T =

@(x,y)5/x^2-1+y+0.5*x^2/y


>> T(exp(pi),nthroot(5,4))
ans =

1.795571125088599e+02



Back to the top

## Exercise 5

>> f = @(x) cos(x)
f =

@(x)cos(x)


>> g = @(x) x^2
g =

@(x)x^2


>> h = @(x) f(x)*g(x)
h =

@(x)f(x)*g(x)


>> fp = @(x) -sin(x)
fp =

@(x)-sin(x)


>> gp = @(x) 2*x
gp =

@(x)2*x


>> hp = @(x) f(x)*gp(x) + fp(x)*g(x)
hp =

@(x)f(x)*gp(x)+fp(x)*g(x)


>> hp(-4.561)
ans =

-19.189089745181988



Back to the top

## Exercise 6

>> distance = @(x1,y1,x2,y2) sqrt((x1-x2)^2 + (y1-y2)^2)
distance =

@(x1,y1,x2,y2)sqrt((x1-x2)^2+(y1-y2)^2)


>> distance(0,cos(0),pi,cos(pi))
ans =

3.724191778237173



Back to the top

## Exercise 7

• Earth : 1.0832e+12 or 1.08 trillion cubic meters
• Mars : 1.63190e+11 or 163 billion cubic meters
• Jupiter : 1.4313e+15 or 1.43 quadrillion cubic meters

Back to the top

## Exercise 8

Density of dry air at sea level for different temperatures

• 0 C : 1.2922
• 10 C : 1.2466
• 20 C : 1.2041
• 30 C : 1.1644

Back to the top

## Get the code

Do you want to try the above code fragments on your own? Download the Matlab script that produces this page here. (lab_2_solns.m) Published with MATLAB® 8.5