Vous êtes sur la page 1sur 4

UNIVERSITY OF JORDAN

COMPUTER ENGINEERING DEPARTMENT


CPE0907311 COMPUTER APPLICATIONS LAB
LAB4 – FUNCTIONS AND SCRIPT FILES 
NAME: ID NUMBER: SECTION:
 

OBJECTIVES
This lab aims to introduce the students to:
1. Common mathematical functions in Matlab.
2. Matlab script files.
3. Creating user-defined functions.
4. Importing and exporting data from and to different data files.
PROBLEMS
Solve the following problems using Matlab. Write your programs using the editor and save all the
files in a folder on the desktop. Name the folder with your student ID number.

1. The capacitance of two parallel conductors of length L and radius r, separated by a distance d
is given by
πε L
C=
⎛d−r⎞
ln ⎜ ⎟
⎝ r ⎠
where ε is the permittivity of air ( ε =8.854x10-12 F/m).
Write a script file that accepts user input for C, L, and r, and computes and displays the
distance value d that gives in the specified capacitance value C. Test your file with L = 0.5 m,
r = 0.01 m, and C = 1 µF.

Script File
Code

The
distance d
is

 
Prepared by Dr. Iyad Jafar
UNIVERSITY OF JORDAN
COMPUTER ENGINEERING DEPARTMENT
CPE0907311 COMPUTER APPLICATIONS LAB
LAB4 – FUNCTIONS AND SCRIPT FILES 
NAME: ID NUMBER: SECTION:
 

2. Repeat Problem 1 by writing a Matlab function that accepts the input values C, r, and L in its
argument and returns calculated value d. You function should be written such that it is
capable of processing single and multiple values for C, r, and L, i.e. it should operate on
vectors. Test your function with the following values C = [2 mF , 0.1 mF], L = [0.2 m 0.1m],
and r = [0.05 and 0.02].

Function
Code

Distance

3. Use a text editor to create a file containing the data given below. Write a Matlab function that
accepts the name of the file as a string and returns the content of the file and the minimum
and maximum values of each row in three output arguments: data,minValue, and maxValue.
23 , -91, 73, 2
33, 14, 5.43, 3
15, 2, 32, -21
-9, 3.3, 1, 11

Code

 
Prepared by Dr. Iyad Jafar
UNIVERSITY OF JORDAN
COMPUTER ENGINEERING DEPARTMENT
CPE0907311 COMPUTER APPLICATIONS LAB
LAB4 – FUNCTIONS AND SCRIPT FILES 
NAME: ID NUMBER: SECTION:
 

4. Enter and save the data given in problem 4 in a spreadsheet. Write a script file to import the
data to a MATLAB variable M then displays the sum of elements of the loaded data on the
command prompt.

Script
File
Code

Answer

5. SOLVE AT HOME AS EXERCISE !

Write a function to find the coefficients of the polynomial y = ax3 + bx2 + cx + d. The
functions should accept a set of four data points (xi,yi), i=1,2,3,4 and solve four linear
equations in terms of the four unknowns a,b,c, and d. Test your function for the case where
(xi,yi)= (-2,-20), (0,4), (2,68), and (4,508), whose answer is a = 7, b=5, c = -6, and d = 4.

6. SOLVE AT HOME AS EXERCISE !

A company wants to locate a distribution center that will serve six of its major customers
in a 30x30 mi area. The locations of the customers relative to the southwest corner of the area
are given in the following table in terms of (x,y) coordinates (the x direction is east; the y
direction is north) (see figure below). Also given is the volume in tons per week that must be
delivered from the distribution center each customer. The weekly delivery cost ci for customer
i depends on the volume Vi and distance di from the distribution center. For simplicity we will
assume that the distance is a straight line distance. The weekly cost ci = 0.5 Vi * di ; i =
1,2,…,6. Formulate the total cost function for all customers then use MATLAB to find the
optimal location for the distribution center (the location that produces the minimum total
weekly cost). Hint: formulate the objective function as a function handle and then use the
fminsearch function.

x Location y Location Volume


Customer
(miles (miles) (tons/week)
1 1 28 3
2 7 18 7
3 8 16 4
4 17 2 5
5 22 10 2
6 27 8 6
 

 
Prepared by Dr. Iyad Jafar
UNIVERSITY OF JORDAN
COMPUTER ENGINEERING DEPARTMENT
CPE0907311 COMPUTER APPLICATIONS LAB
LAB4 – FUNCTIONS AND SCRIPT FILES 
NAME: ID NUMBER: SECTION:
 

30
1

25

20 2

y (miles)
3
15

5
10 6

5
4

0
0 5 10 15 20 25 30
x (miles)

  

 
Prepared by Dr. Iyad Jafar