Скачать презентацию An Introduction to MATLAB Here is a

An Introduction to MATLAB(home_task).pptx

• Количество слайдов: 34

An Introduction to MATLAB

Here is a complete program, illustrating while, if, else, and end, that uses interval bisection to find a zero of a polynomial: a = 0; fa = -Inf; b = 3; fb = Inf; while b-a > eps*b x = (a+b)/2; fx = x^3 -2*x-5; if sign(fx) == sign(fa) a = x; fa = fx; else b = x; fb = fx; end X The break statement lets you exit early from a for loop or while loop. In nested loops, break exits from the innermost loop only. a = 0; fa = -Inf; b = 3; fb = Inf; while b-a > eps*b x = (a+b)/2; fx = x^3 -2*x-5; if fx == 0 break elseif sign(fx) == sign(fa) a = x; fa = fx; Else b = x; fb = fx; The result is a root of the polynomial x 3 - end 2 x - 5, namely End x = 2. 09455148154233 x

continue • The continue statement passes control to the next iteration of the for loop or while loop in which it appears, skipping any remaining statements in the body of the loop. The same holds true for continue statements in nested loops. That is, execution continues at the beginning of the loop in which the continue statement was encountered.

• The example shows a continue loop that counts the number of equations that have real roots, skipping all equations that have only imaginaries roots. • A continue statement is used to advance to the next equation without incrementing the count : function k=number(a, b, c); %This function uses for finding number of the equations % a, b , c- coefficients of the equation k=0; for i=1: 10 a=a+1; b=b+2; c=c+3; d=b^2 -4*a*c; if d<0 continue else k=k+1; end k Return >>number(10, -27, 5); k =2

Program Termination — return • Program termination control enables to exit from program at some point prior to its normal termination point. • return terminates the current sequence of commands and returns control to the invoking function or to the keyboard. return is also used to terminate keyboard mode. • A called function normally transfers control to the function that invoked it when it reaches the end of the function. You can insert a return statement within the called function to force an early termination and to transfer control to the invoking function.

HOME TASK • • • Do Matlab script-file for solution of the equation (choose your variant according to your roll number in the attendance book) using 1 - Bisectional Method 2 - Newton-Raphson Method 3 - Secant Method 4 - Fixed Point Iteration Method 5 - In-built Function

Requirements to the script-file 1. Must be building of the graph of your function 2. Must be a routine dialog with the user 3. Must be checking of the input values 4. Must be a choice of the method for solution 5. Must be the comments for all using functions NOTE. The function of the equation should be created and saved as m-file

The example how to complete home task

The function of the equation function fx = f_eq( x ) %The function of the equation fx=x. ^3+7*x+3; end The function should be saved in file named f_eq. m

Script-file example( part 1) clc; clear; % function [email protected]_eq; % graph x=-5: 0. 001: 5; y=f(x); plot(x, y) grid on

The explanations to part 1 • Clc - clears all input and output from the Command Window display, giving you a "clean screen. " After using clc, you cannot use the scroll bar to see the history of functions, but you still can use the up arrow to recall statements from the command history. • Clear -removes all variables from the workspace, releasing them from system memory.

The explanations to part 1 • [email protected] f_eq; • handle = @ functionname returns a handle to the specified MATLAB function. A function handle is a MATLAB value that provides a means of calling a function indirectly. You can pass function handles in calls to other functions (often called functions). A function handle is one of the standard MATLAB data types.

Example 1 — Constructing a Handle to a Named Function • The following example creates a function handle for the humps function and assigns it to the variable fhandle. • fhandle = @humps; • Pass the handle to another function in the same way you would pass any argument. This example passes the function handle just created to fminbnd, which then minimizes over the interval [0. 3, 1]. • x = fminbnd(fhandle, 0. 3, 1) • x = 0. 6370

Example 2 — Constructing a Handle to an Anonymous Function • The statement below creates an anonymous function that finds the square of a number. When you call this function, MATLAB assigns the value you pass in to variable x, and then uses x in the equation x. ^2: • sqr = @(x) x. ^2; • The @ operator constructs a function handle for this function, and assigns the handle to the output variable sqr. • To execute the sqr function defined above, type • a = sqr(5) • a = 25

The result of part 1

Script-file example( part 2) % interval a=input(' input endpoint of interval n'); b=input(' input endpoint of intervaln');

The explanations to part 2 • a=input(' input border point of interval n'); • eval. Response = input(prompt) displays the prompt string on the screen, waits for input from the keyboard, evaluates any expressions in the input, and returns the value in eval. Response. To evaluate expressions, the input function accesses variables in the current workspace. • str. Response = input(prompt, 's') returns the entered text as a MATLAB string, without evaluating expressions. • Remarks • If you press the Return key without entering anything, input returns an empty matrix. • To create a prompt that spans several lines, use 'n' to indicate each new line.

The result of part 2 >>input endpoint of interval -2 >> input endpoint of interval 2

Script-file example( part 3) if f(a)*f(b)>0 error ('The function values must differ in sign! Try again'); end; • The explanations to part 3 • error ('The function values must differ in sign! Try again'); Display message and abort function

The result of part 3 >>input endpoint of interval -2 >> input endpoint of interval -3 ? ? ? Error using ==> example at 16 The function values must differ in sign! Try again >>

Script-file example( part 4) str 1=input(' 1 - Bisectional Method'); str 2=input(' 2 - Newton-Raphson Method'); str 3=input(' 3 - Secant Method'); str 4=input(' 4 - Fixed Point Iteration Method'); str 5=input(' 5 - In-built Functionn');

The result of part 4 input endpoint of interval -2 input endpoint of interval 2 1 - Bisectional Method 2 - Newton-Raphson Method 3 - Secant Method 4 - Fixed Point Iteration Method 5 - In-built Function

Script-file example( part 5) prompt = {'Enter the name of method: , 'Enter the error of calculation: '}; dlg_title = 'Input values for solution of equation'; num_lines = 1; def = {'1', '1 E-5'}; answer = inputdlg(prompt, dlg_title, num_lines, def); NUM=str 2 double(answer(1)); e=str 2 double(answer(2));

The explanations to part 5 answer = inputdlg(prompt) creates a modal dialog box and returns user input for multiple prompts in the cell array. prompt is a cell array containing prompt strings.

Example 1 Create a dialog box to input an integer and colormap name. Allow one line for each value. • prompt = {'Enter matrix size: ', 'Enter colormap name: '}; • dlg_title = 'Input for peaks function'; num_lines = 1; • def = {'20', 'hsv'}; • answer = inputdlg(prompt, dlg_title, num_lines, def);

Script-file example( part 6) switch NUM case 4 % Fixed Point Iteratation % symbolic variable syms x; if (abs(subs(diff(sym(f(x))), 'x', a))>1) l 1=1/abs(subs(diff(sym(f(x))), 'x', a)); else l 1=1; end if (abs(subs(diff(sym(f(x))), 'x', b))>1) l 2=1/abs(subs(diff(sym(f(x))), 'x', b)); else l 2=1; End if (l 1

The explanations to part 6 abs- absolute value and complex magnitude >>abs(-5) ans = 5 >>abs(3+4 i) ans = 5 Subs- symbolic substitution in symbolic expression or matrix Single Substitution >>syms a b; >>subs(a + b, a, 4) returns ans = b + 4 Multiple Substitutions >>syms a b; >>cos(a) + sin(b), {a, b}, {sym('alpha'), 2}) returns ans = sin(2) + cos(alpha)

The explanations to part 6 • Diff- differences and approximate derivatives Example >>a=-2; >> [email protected]_eq; x. ^3+7*x+3; >> syms x; >> abs(subs(diff(sym(f(x))), 'x', a)) ans = 19

Script-file example( part 6) [email protected](x) x-l*f(x); x 1=(a+b)/2; x 2=fi(x 1); i=0; while (abs(x 2 -x 1)>e) x 1=x 2; x 2=fi(x 1); i=i+1; end disp(['Fixed point iteration x = ' num 2 str(x 2) ' f = ' num 2 str(f(x 2)) ' i = ' num 2 str(i)])

The explanations to part 6 • Num 2 str -convert number to string Syntax • he num 2 str function converts numbers to their string representations. This function is useful for labeling and titling plots with numeric values. • str = num 2 str(A) converts array A into a string representation str. Converted floating-point values have a maximum of four digits of precision, and an exponent if required. For integer values, num 2 str returns the exact string representation of the value. • str = num 2 str(A, precision) converts the array A into a string representation str with the maximum number of digits specified by precision. • str = num 2 str(A, format) converts array A using the supplied format, as described on the fprintf function reference page. By default, num 2 str displays floating point values using the '%11. 4 g' format (four significant digits in exponential or fixed-point notation, whichever is shorter). Example A = num 2 str(x, '%10. 5 en') A = 6. 87255 e+003 1. 55597 e+003 8. 55890 e+003 % Convert to string array.

Script-file example( part 6) case 5 % in-built function [x, fval] = fzero(f, [a b]); disp(['In-built function x= ' num 2 str(x) ' f = ' num 2 str(fval) ]); end return

The results if NUM=4 input endpoint of interval -2 input endpoint of interval 2 1 - Bisectional Method 2 - Newton-Rafson Method 3 - Secant Method 4 - Fixed Point Iteration Method 5 - In-built Function Fixed point iteration х = -0. 41812 f = 8. 7056 e-005 i = 20

The results if NUM=5 input border point of interval -2 input border point of interval 2 1 - Bisectional Method 2 - Newton-Rafson Method 3 - Secant Method 4 - Fixed Point Iteration Method 5 - In-built Function In-built function х = -0. 41813 f = 0