numerical methods lecture 1 - vsb.czhomen.vsb.cz/~lud0016/nm/nm1.pdf · matlab = matrix laboratory...

42
Numerical Methods – Lecture 1 Basics of MATLAB by Pavel Ludvík

Upload: dangduong

Post on 11-Jul-2018

235 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Numerical Methods Lecture 1 - vsb.czhomen.vsb.cz/~lud0016/NM/NM1.pdf · MATLAB = MATrix LABoratory MATLAB was devised by Cleve Moler in the late 1970’s to make numerical computing

Numerical Methods – Lecture 1

Basics of MATLAB

by Pavel Ludvík

Page 2: Numerical Methods Lecture 1 - vsb.czhomen.vsb.cz/~lud0016/NM/NM1.pdf · MATLAB = MATrix LABoratory MATLAB was devised by Cleve Moler in the late 1970’s to make numerical computing

The recommended textbook:

Numerical Methods – Lecture 1 by Pavel Ludvík 2 / 30

Page 3: Numerical Methods Lecture 1 - vsb.czhomen.vsb.cz/~lud0016/NM/NM1.pdf · MATLAB = MATrix LABoratory MATLAB was devised by Cleve Moler in the late 1970’s to make numerical computing

The recommended textbook:

Title: Numerical methods with worked examples:Matlab edition

Authors: WOODFORD, Chris a Chris PHILLIPSPublisher: New York: Springer, c2012

Numerical Methods – Lecture 1 by Pavel Ludvík 2 / 30

Page 4: Numerical Methods Lecture 1 - vsb.czhomen.vsb.cz/~lud0016/NM/NM1.pdf · MATLAB = MATrix LABoratory MATLAB was devised by Cleve Moler in the late 1970’s to make numerical computing

Basics of MATLAB

Numerical Methods – Lecture 1 by Pavel Ludvík 3 / 30

Page 5: Numerical Methods Lecture 1 - vsb.czhomen.vsb.cz/~lud0016/NM/NM1.pdf · MATLAB = MATrix LABoratory MATLAB was devised by Cleve Moler in the late 1970’s to make numerical computing

MATLAB = MATrix LABoratory

MATLAB was devised by Cleve Moler in the late 1970’s to makenumerical computing easier for students at the University ofNew Mexico. Many of the obstacles to using a computer formathematics were removed. In a MATLAB program variables,whether real or complex numbers, vectors or matrices may benamed and used as and when required without priornotification or declaration and may be manipulated accordingto the rules of mathematics. MATLAB spread to otherUniversities and in 1984 Cleve went into partnership with acolleague to set up a company called Mathworks to marketMATLAB. Mathworks is now a multi-national corporationspecialising in technical computing software. MATLAB andproducts built on MATLAB are used all over the world byinnovative technology companies, government research labs,financial institutions, and more than 3,500 universities.

Numerical Methods – Lecture 1 by Pavel Ludvík 4 / 30

Page 6: Numerical Methods Lecture 1 - vsb.czhomen.vsb.cz/~lud0016/NM/NM1.pdf · MATLAB = MATrix LABoratory MATLAB was devised by Cleve Moler in the late 1970’s to make numerical computing

Windows in MATLAB and Their Function

Numerical Methods – Lecture 1 by Pavel Ludvík 5 / 30

Page 7: Numerical Methods Lecture 1 - vsb.czhomen.vsb.cz/~lud0016/NM/NM1.pdf · MATLAB = MATrix LABoratory MATLAB was devised by Cleve Moler in the late 1970’s to make numerical computing

Creating Variables and Using BasicArithmetic

1. To create a single variable just use it on the left hand sideof an equal sign. Enter the name of the variable to see itscurrent value.

2. The ordinary rules of arithmetic apply using the symbols+, ?, and for addition, subtraction, multiplication and divi-sion and raising to a power. Use round brackets ( and ) andmind priorities of operations.

Try to write several command on the same line separatingthem by a comma or a semi-colon. What is the difference?

Numerical Methods – Lecture 1 by Pavel Ludvík 6 / 30

Page 8: Numerical Methods Lecture 1 - vsb.czhomen.vsb.cz/~lud0016/NM/NM1.pdf · MATLAB = MATrix LABoratory MATLAB was devised by Cleve Moler in the late 1970’s to make numerical computing

Creating Variables and Using BasicArithmetic

ExerciseEnter the following commands on separate lines.1. r=42. ab1=7.1+3.23. r

4. A=r∧25. sol=ab1*(1+1/r)

6. ab1=sol∧(1/2)7. CV2=10/38. x=1.5e-2

Try to write several command on the same line separatingthem by a comma or a semi-colon. What is the difference?

Numerical Methods – Lecture 1 by Pavel Ludvík 6 / 30

Page 9: Numerical Methods Lecture 1 - vsb.czhomen.vsb.cz/~lud0016/NM/NM1.pdf · MATLAB = MATrix LABoratory MATLAB was devised by Cleve Moler in the late 1970’s to make numerical computing

Creating Variables and Using BasicArithmetic

ExerciseEnter the following commands on separate lines.1. r=42. ab1=7.1+3.23. r

4. A=r∧25. sol=ab1*(1+1/r)

6. ab1=sol∧(1/2)7. CV2=10/38. x=1.5e-2

Try to write several command on the same line separatingthem by a comma or a semi-colon. What is the difference?

Numerical Methods – Lecture 1 by Pavel Ludvík 6 / 30

Page 10: Numerical Methods Lecture 1 - vsb.czhomen.vsb.cz/~lud0016/NM/NM1.pdf · MATLAB = MATrix LABoratory MATLAB was devised by Cleve Moler in the late 1970’s to make numerical computing

Standard Functions

MATLAB provides a large number of commonly used functionsincluding abs, sqrt, exp, log and sin, cos and tan and inversesasin, acos, atan.

ExerciseUse MATLAB to calculate the following.1. x=sqrt(2)2. A=sin(1.5)3. a=sqrt(x∧2+A∧2)4. y=exp(1/x)

Look up the entry Elementary Math in MATLAB’s help.

Numerical Methods – Lecture 1 by Pavel Ludvík 7 / 30

Page 11: Numerical Methods Lecture 1 - vsb.czhomen.vsb.cz/~lud0016/NM/NM1.pdf · MATLAB = MATrix LABoratory MATLAB was devised by Cleve Moler in the late 1970’s to make numerical computing

Standard Functions

MATLAB provides a large number of commonly used functionsincluding abs, sqrt, exp, log and sin, cos and tan and inversesasin, acos, atan.

ExerciseUse MATLAB to calculate the following.1. x=sqrt(2)2. A=sin(1.5)3. a=sqrt(x∧2+A∧2)4. y=exp(1/x)

Look up the entry Elementary Math in MATLAB’s help.

Numerical Methods – Lecture 1 by Pavel Ludvík 7 / 30

Page 12: Numerical Methods Lecture 1 - vsb.czhomen.vsb.cz/~lud0016/NM/NM1.pdf · MATLAB = MATrix LABoratory MATLAB was devised by Cleve Moler in the late 1970’s to make numerical computing

Standard Functions

MATLAB provides a large number of commonly used functionsincluding abs, sqrt, exp, log and sin, cos and tan and inversesasin, acos, atan.

ExerciseUse MATLAB to calculate the following.1. x=sqrt(2)2. A=sin(1.5)3. a=sqrt(x∧2+A∧2)4. y=exp(1/x)

Look up the entry Elementary Math in MATLAB’s help.

Numerical Methods – Lecture 1 by Pavel Ludvík 7 / 30

Page 13: Numerical Methods Lecture 1 - vsb.czhomen.vsb.cz/~lud0016/NM/NM1.pdf · MATLAB = MATrix LABoratory MATLAB was devised by Cleve Moler in the late 1970’s to make numerical computing

Standard Functions

Exercise

1. Find the longest side of a right angled triangle whose othersides have lengths 12 and 5. Use the Pythagoras Theorem.

2. Find the roots of the quadratic function 3x2 − 13x + 4 usingthe standard formula.

ExerciseTry to find the values of constants π and e in MATLAB.

Numerical Methods – Lecture 1 by Pavel Ludvík 8 / 30

Page 14: Numerical Methods Lecture 1 - vsb.czhomen.vsb.cz/~lud0016/NM/NM1.pdf · MATLAB = MATrix LABoratory MATLAB was devised by Cleve Moler in the late 1970’s to make numerical computing

Vectors and MatricesThe matrix

A =

2 3 −14 8 −3−2 3 1

is created by

A = [2 3 -1; 4 8 -3; -2 3 1].

Exercise

1. Create the row vector x = (4,10,−1,0).2. Create the column vector y = (−5.3,−2,0.9,1)ᵀ.3. Create the matrix

B =

1 7.3 −5.6 21.4 8 −3 0−2 6.3 1 −2

.

Numerical Methods – Lecture 1 by Pavel Ludvík 9 / 30

Page 15: Numerical Methods Lecture 1 - vsb.czhomen.vsb.cz/~lud0016/NM/NM1.pdf · MATLAB = MATrix LABoratory MATLAB was devised by Cleve Moler in the late 1970’s to make numerical computing

Vectors and MatricesThe matrix

A =

2 3 −14 8 −3−2 3 1

is created by

A = [2 3 -1; 4 8 -3; -2 3 1].

Exercise

1. Create the row vector x = (4,10,−1,0).2. Create the column vector y = (−5.3,−2,0.9,1)ᵀ.3. Create the matrix

B =

1 7.3 −5.6 21.4 8 −3 0−2 6.3 1 −2

.

Numerical Methods – Lecture 1 by Pavel Ludvík 9 / 30

Page 16: Numerical Methods Lecture 1 - vsb.czhomen.vsb.cz/~lud0016/NM/NM1.pdf · MATLAB = MATrix LABoratory MATLAB was devised by Cleve Moler in the late 1970’s to make numerical computing

Vectors and Matrices

A =

2 3 −14 8 −3−2 3 1

, B =

1 7.3 −5.6 21.4 8 −3 0−2 6.3 1 −2

Find out what happens if you type A(1,2), A(:,1) andA(3,:).Figure out what mathematical operations can performedwith matrices A and B.

Numerical Methods – Lecture 1 by Pavel Ludvík 10 / 30

Page 17: Numerical Methods Lecture 1 - vsb.czhomen.vsb.cz/~lud0016/NM/NM1.pdf · MATLAB = MATrix LABoratory MATLAB was devised by Cleve Moler in the late 1970’s to make numerical computing

Vectors and Matrices

ExerciseWrite a linear system

3x1 + 5x2 + 7x3 = 25,x1 − 4x2 + 2x3 = 10,4x1 − x2 − 3x3 = −1

in a matrix form asAx = b

and solve it using MATLAB (realize that a notion of a inversematrix can be employed).

Use the command x=A\b and compare it with the previous re-sult.

Numerical Methods – Lecture 1 by Pavel Ludvík 11 / 30

Page 18: Numerical Methods Lecture 1 - vsb.czhomen.vsb.cz/~lud0016/NM/NM1.pdf · MATLAB = MATrix LABoratory MATLAB was devised by Cleve Moler in the late 1970’s to make numerical computing

Vectors and Matrices

ExerciseWrite a linear system

3x1 + 5x2 + 7x3 = 25,x1 − 4x2 + 2x3 = 10,4x1 − x2 − 3x3 = −1

in a matrix form asAx = b

and solve it using MATLAB (realize that a notion of a inversematrix can be employed).

Use the command x=A\b and compare it with the previous re-sult.

Numerical Methods – Lecture 1 by Pavel Ludvík 11 / 30

Page 19: Numerical Methods Lecture 1 - vsb.czhomen.vsb.cz/~lud0016/NM/NM1.pdf · MATLAB = MATrix LABoratory MATLAB was devised by Cleve Moler in the late 1970’s to make numerical computing

Defining Mathematical Functions

In most situations, we will define the mathematical functions inthe following way:Consider the functions

f (x) = x2 − sin(2x),

g(x,y) = x2 + y2 − cos2(xy).

You can define them by� �f=@(x)x^2-sin(2*x)g=@(x,y)x^2+y^2-cos(x*y)^2

Numerical Methods – Lecture 1 by Pavel Ludvík 12 / 30

Page 20: Numerical Methods Lecture 1 - vsb.czhomen.vsb.cz/~lud0016/NM/NM1.pdf · MATLAB = MATrix LABoratory MATLAB was devised by Cleve Moler in the late 1970’s to make numerical computing

PlottingCommand plot(x,y)� �x=[1 3 4 12]y=[-1 0 2 -3]plot(x,y)grid on Command fplot(f,[a,b])� �f=@(x) x^2-2*xg=@(x) sin(3*x)fplot(f,[-1 5])hold on, grid onfplot(g,[-1 5],’r’) Look up plot and fplot in help of MATLAB.

Numerical Methods – Lecture 1 by Pavel Ludvík 13 / 30

Page 21: Numerical Methods Lecture 1 - vsb.czhomen.vsb.cz/~lud0016/NM/NM1.pdf · MATLAB = MATrix LABoratory MATLAB was devised by Cleve Moler in the late 1970’s to make numerical computing

Plotting Data and Functions

Exercises

1. Plot the functions y = x and y = sin(x) and y = cos(x) overthe range [0,2π] on the same graph but using different co-lours.

2. Plot the three dimensional surface defined by z = x2 − y2

across the x − y ranges [−2,2] on both axes.

Numerical Methods – Lecture 1 by Pavel Ludvík 14 / 30

Page 22: Numerical Methods Lecture 1 - vsb.czhomen.vsb.cz/~lud0016/NM/NM1.pdf · MATLAB = MATrix LABoratory MATLAB was devised by Cleve Moler in the late 1970’s to make numerical computing

Solution of Exercise 2

� �% establish a grid of points equally spaced at

intervals of 0.1 across the range[x, y] = meshgrid(-2 : 0.1 : 2, -2 : 0.1 : 2);% establish z-values using element by element

multiplicationz = x.*x - y.*y % plot the surfacesurf(x, y, z);colorbar % show the colour scale.

Numerical Methods – Lecture 1 by Pavel Ludvík 15 / 30

Page 23: Numerical Methods Lecture 1 - vsb.czhomen.vsb.cz/~lud0016/NM/NM1.pdf · MATLAB = MATrix LABoratory MATLAB was devised by Cleve Moler in the late 1970’s to make numerical computing

M-Files

On the whole entering MATLAB commands and statements lineby line is too error prone for anything but very short, transitoryprograms. Programs under development and programs whichare going to be used repeatedly are better stored as asequence of commands in a file. Such files are called M-files.

Exercise

1. Use the file menu to create an M-file. Enter a sequenceof commands. Save the file using a name of your choice.Execute the newly created M-file. Edit the file if necessaryto obtain successful execution, then close the file.

2. Re-open the M-file, make changes to the program and re-execute.

Numerical Methods – Lecture 1 by Pavel Ludvík 16 / 30

Page 24: Numerical Methods Lecture 1 - vsb.czhomen.vsb.cz/~lud0016/NM/NM1.pdf · MATLAB = MATrix LABoratory MATLAB was devised by Cleve Moler in the late 1970’s to make numerical computing

M-Files

On the whole entering MATLAB commands and statements lineby line is too error prone for anything but very short, transitoryprograms. Programs under development and programs whichare going to be used repeatedly are better stored as asequence of commands in a file. Such files are called M-files.

Exercise

1. Use the file menu to create an M-file. Enter a sequenceof commands. Save the file using a name of your choice.Execute the newly created M-file. Edit the file if necessaryto obtain successful execution, then close the file.

2. Re-open the M-file, make changes to the program and re-execute.

Numerical Methods – Lecture 1 by Pavel Ludvík 16 / 30

Page 25: Numerical Methods Lecture 1 - vsb.czhomen.vsb.cz/~lud0016/NM/NM1.pdf · MATLAB = MATrix LABoratory MATLAB was devised by Cleve Moler in the late 1970’s to make numerical computing

The colon Notation and the for Loop

How the colon worksk:n is equivalent to k,2,3,4, . . . ,n. (incrementing by 1)k:m:n is equivalent to k,k + m,k + 2m,k + 3m, . . . ,n. (incre-menting by m)

We use the for construct to sum the first 10 integers(1,2, . . .10):� �addnumbers = 0; % initial totalfor i = 1 : 10;addnumbers = addnumbers + i; % running totalend

Numerical Methods – Lecture 1 by Pavel Ludvík 17 / 30

Page 26: Numerical Methods Lecture 1 - vsb.czhomen.vsb.cz/~lud0016/NM/NM1.pdf · MATLAB = MATrix LABoratory MATLAB was devised by Cleve Moler in the late 1970’s to make numerical computing

The colon Notation and the for Loop

Exercises

1. By making a small change to the program above, useMATLAB to find the sum of the even numbers 2,4, . . . ,10.

2. The following series may be used to calculate an approxi-mation to

√2

xn+1 =xn

2+

1xn

, n = 1,2, . . . , x1 = 1.

Calculate x1,x2, . . . ,x7 and show convergence to√

2.

Numerical Methods – Lecture 1 by Pavel Ludvík 18 / 30

Page 27: Numerical Methods Lecture 1 - vsb.czhomen.vsb.cz/~lud0016/NM/NM1.pdf · MATLAB = MATrix LABoratory MATLAB was devised by Cleve Moler in the late 1970’s to make numerical computing

The colon Notation and the for Loop

The following code would be sufficient to solve problem no. 2:� �x(1) = 1 % first approximationfor n = 1 : 6x(n+1) = x(n)/2 + 1/x(n) % successive approximationsend ExerciseDevise an alternative, more economical (with respect to thecomputer memory) version which does not save intermediateresults but simply displays and then overwrites the current es-timate of

√2 by the next estimate.

Numerical Methods – Lecture 1 by Pavel Ludvík 19 / 30

Page 28: Numerical Methods Lecture 1 - vsb.czhomen.vsb.cz/~lud0016/NM/NM1.pdf · MATLAB = MATrix LABoratory MATLAB was devised by Cleve Moler in the late 1970’s to make numerical computing

The colon Notation and the for Loop

ExercisesThe Fibonacci numbers: 0,1,1,2,3,5,8,13, . . . (each number isthe sum of the two preceding numbers) relate to several na-tural phenomena. Use a for loop to print the first 20 Fibonaccinumbers.

Numerical Methods – Lecture 1 by Pavel Ludvík 20 / 30

Page 29: Numerical Methods Lecture 1 - vsb.czhomen.vsb.cz/~lud0016/NM/NM1.pdf · MATLAB = MATrix LABoratory MATLAB was devised by Cleve Moler in the late 1970’s to make numerical computing

The if Construct

In addition to the loop structure program flow may be cont-rolled using if, else and elseif. Trace the basic if construct inthe help of MATLAB.

QuestionWhat kind of logical conjunctions do you know?

Numerical Methods – Lecture 1 by Pavel Ludvík 21 / 30

Page 30: Numerical Methods Lecture 1 - vsb.czhomen.vsb.cz/~lud0016/NM/NM1.pdf · MATLAB = MATrix LABoratory MATLAB was devised by Cleve Moler in the late 1970’s to make numerical computing

The if Construct

In addition to the loop structure program flow may be cont-rolled using if, else and elseif. Trace the basic if construct inthe help of MATLAB.

QuestionWhat kind of logical conjunctions do you know?

Numerical Methods – Lecture 1 by Pavel Ludvík 21 / 30

Page 31: Numerical Methods Lecture 1 - vsb.czhomen.vsb.cz/~lud0016/NM/NM1.pdf · MATLAB = MATrix LABoratory MATLAB was devised by Cleve Moler in the late 1970’s to make numerical computing

Enter the code in a MATLAB program to verify the outcome.� �x = 2;y = 1;if x > 2; y = x; end; � �a = 1;b = 2;if a == 1; b = a + 1; end; � �u = 1; v = 2; w = -2;if (u ~= 0 & u < abs(w) ) | u < 0;u = u + w;elseu = u - w;end;

Numerical Methods – Lecture 1 by Pavel Ludvík 22 / 30

Page 32: Numerical Methods Lecture 1 - vsb.czhomen.vsb.cz/~lud0016/NM/NM1.pdf · MATLAB = MATrix LABoratory MATLAB was devised by Cleve Moler in the late 1970’s to make numerical computing

The if Construct

ExerciseIn using the formula for the solution of a quadratic equationthe discriminant b2 − 4ac may be positive (indicating two realroots), zero (a double root) or negative (complex roots). WriteMATLAB code to set a variable roots to 2, 1 or 0 depending onthe value of the discriminant. Test the program using variousvalues of a, b and c.

ExerciseGiven a vector (or matrix) of dimension (m,n) use a for loopwithin a for loop to count the number of non-zero elements.Test your code on simple examples.

Numerical Methods – Lecture 1 by Pavel Ludvík 23 / 30

Page 33: Numerical Methods Lecture 1 - vsb.czhomen.vsb.cz/~lud0016/NM/NM1.pdf · MATLAB = MATrix LABoratory MATLAB was devised by Cleve Moler in the late 1970’s to make numerical computing

The if Construct

ExerciseIn using the formula for the solution of a quadratic equationthe discriminant b2 − 4ac may be positive (indicating two realroots), zero (a double root) or negative (complex roots). WriteMATLAB code to set a variable roots to 2, 1 or 0 depending onthe value of the discriminant. Test the program using variousvalues of a, b and c.

ExerciseGiven a vector (or matrix) of dimension (m,n) use a for loopwithin a for loop to count the number of non-zero elements.Test your code on simple examples.

Numerical Methods – Lecture 1 by Pavel Ludvík 23 / 30

Page 34: Numerical Methods Lecture 1 - vsb.czhomen.vsb.cz/~lud0016/NM/NM1.pdf · MATLAB = MATrix LABoratory MATLAB was devised by Cleve Moler in the late 1970’s to make numerical computing

The while Loop

The while loop repeats a sequence of instructions until a spe-cified condition is met. It is used as an alternative to the forloop in cases where the number of required repeats is notknown in advance. Trace the basic while construct in the helpof MATLAB.

ExerciseCreate a vector of numbers which contains one or more zeros.Construct a while loop to find the sum of the numbers up tothe first zero.

Numerical Methods – Lecture 1 by Pavel Ludvík 24 / 30

Page 35: Numerical Methods Lecture 1 - vsb.czhomen.vsb.cz/~lud0016/NM/NM1.pdf · MATLAB = MATrix LABoratory MATLAB was devised by Cleve Moler in the late 1970’s to make numerical computing

The while Loop

The while loop repeats a sequence of instructions until a spe-cified condition is met. It is used as an alternative to the forloop in cases where the number of required repeats is notknown in advance. Trace the basic while construct in the helpof MATLAB.

ExerciseCreate a vector of numbers which contains one or more zeros.Construct a while loop to find the sum of the numbers up tothe first zero.

Numerical Methods – Lecture 1 by Pavel Ludvík 24 / 30

Page 36: Numerical Methods Lecture 1 - vsb.czhomen.vsb.cz/~lud0016/NM/NM1.pdf · MATLAB = MATrix LABoratory MATLAB was devised by Cleve Moler in the late 1970’s to make numerical computing

Simple Screen Output and Keyboard Input

Screen Output

As an alternative to entering the name of a variable thefunction disp may be used to output a value or a string. Thestandard forms are disp(x) and disp(’s’) where x is a variableand s is a string of alphanumeric and other characters.

Keyboard Input

The function input may be used to request user input. Thestandard form is x = input(’request’). The user sees the cha-racter string request on the screen and enters number(s) fromthe keyboard to be allocated to the program variable x.

Numerical Methods – Lecture 1 by Pavel Ludvík 25 / 30

Page 37: Numerical Methods Lecture 1 - vsb.czhomen.vsb.cz/~lud0016/NM/NM1.pdf · MATLAB = MATrix LABoratory MATLAB was devised by Cleve Moler in the late 1970’s to make numerical computing

Simple Screen Output and Keyboard Input

Screen Output

As an alternative to entering the name of a variable thefunction disp may be used to output a value or a string. Thestandard forms are disp(x) and disp(’s’) where x is a variableand s is a string of alphanumeric and other characters.

Keyboard Input

The function input may be used to request user input. Thestandard form is x = input(’request’). The user sees the cha-racter string request on the screen and enters number(s) fromthe keyboard to be allocated to the program variable x.

Numerical Methods – Lecture 1 by Pavel Ludvík 25 / 30

Page 38: Numerical Methods Lecture 1 - vsb.czhomen.vsb.cz/~lud0016/NM/NM1.pdf · MATLAB = MATrix LABoratory MATLAB was devised by Cleve Moler in the late 1970’s to make numerical computing

Simple Screen Output and Keyboard Input

As part of an interactive program, use the following code toobtain the value of m and echo the input to the screen. Theprogram asks for confirmation (Y or N) that the number iscorrect and if necessary repeats the original request.

� �m = input(’Enter the number of data values \n’);reply = input(’Is that correct, answer Y or N\n’,’s’

);while ~strcmp(reply,’Y’)input(’Enter the number of data values\n’);reply = input(’Is that correct, answer Y or N\n’,’s’

);end;disp(’Thank you’);

Numerical Methods – Lecture 1 by Pavel Ludvík 26 / 30

Page 39: Numerical Methods Lecture 1 - vsb.czhomen.vsb.cz/~lud0016/NM/NM1.pdf · MATLAB = MATrix LABoratory MATLAB was devised by Cleve Moler in the late 1970’s to make numerical computing

Simple Screen Output and Keyboard Input

ExerciseWrite a program to request a password from the user. Com-pare the reply with a stored string. If necessary repeat therequest at most twice until there is an agreement.

Numerical Methods – Lecture 1 by Pavel Ludvík 27 / 30

Page 40: Numerical Methods Lecture 1 - vsb.czhomen.vsb.cz/~lud0016/NM/NM1.pdf · MATLAB = MATrix LABoratory MATLAB was devised by Cleve Moler in the late 1970’s to make numerical computing

User Defined Functions

You can define your own function stored i an M-file. Thefunction heading has the form:

function [out1, out2,. . . ] = functionname (in1, in2,. . . )

and M-file has to be saved under the same name as is a nameof the function.

ExampleSum of the series 1 + x + x2 + · · ·+ xn:� �function [result] = sumGP(x, n)if x == 1; result = n+1 % Check for the exceptional

case.else result = (x^(n+1) - 1)/(x - 1)end

Numerical Methods – Lecture 1 by Pavel Ludvík 28 / 30

Page 41: Numerical Methods Lecture 1 - vsb.czhomen.vsb.cz/~lud0016/NM/NM1.pdf · MATLAB = MATrix LABoratory MATLAB was devised by Cleve Moler in the late 1970’s to make numerical computing

User Defined Functions

ExerciseWrite a function MaxElement to find the largest absolute va-lue of all elements of an array or vector. Test the function onsimple examples.

Numerical Methods – Lecture 1 by Pavel Ludvík 29 / 30

Page 42: Numerical Methods Lecture 1 - vsb.czhomen.vsb.cz/~lud0016/NM/NM1.pdf · MATLAB = MATrix LABoratory MATLAB was devised by Cleve Moler in the late 1970’s to make numerical computing

User Defined Functions

ExerciseWrite a function MaxElement to find the largest absolute va-lue of all elements of an array or vector. Test the function onsimple examples.

Solution (One of Many):� �function [result] = MaxElement(v)result = 0;for i = 1 : length(v)if abs(v(i)) > result; result = abs(v(i));endend

Numerical Methods – Lecture 1 by Pavel Ludvík 30 / 30