Display Format for Numeric Values MATLAB displays numeric


Display Format for Numeric Values MATLAB displays numeric output as 5-digit by default. (fixed,4 point values)

M-File If you include “;” at the end of each statement, result will not be shown immediately Save file as Denem430.m

Plot Function The plot function can be used to draw the relationship between two variables. Format 2D: plot(x,y,lineParameters) X – row vector of x-axis values. Y – row vector of y-axis values. lineParameters – line style, color style, etc.

Example: Plot the values of a random matrix xxx = 1:100; yyy = rand(1,100); plot(xxx,yyy) zzz= xxx.^2; figure, plot(xxx,zzz) The command figure is used to create a new figure window each time a plot is made.

Example: Draw sin(x) clear, close all clc x = 0:pi/36:10*pi; y = sin(x); plot(x,y,’b') Plot data can include NaN and inf values, which cause breaks in the lines drawn.

Trigonometric Basic Function

Line Plot Styles

Color Plot Styles

Example: Plotting the lines using line parameters clear, close all clc plot(xxx,yyy) figure, plot(xxx,yyy,'g:') figure, plot(xxx,yyy,'r--') figure, plot(xxx,yyy,':mo')

Example: Drawing two plots in the same figure clear, close all clc aaa = 1:100 bbb = rand(1,100) plot(xxx,yyy,'r') hold on plot(aaa,bbb,'-.gv') hold off

Drawing different plots in the same figure subplot(2,1,1), plot(xxx,yyy,'r') subplot(2,1,2), plot(aaa,bbb,'-.gv') Subplot – create axes in tiled positions subplot(m,n,p) , “representation” M – row number N – column number P – tile number

Example: Drawing Bar Graphs clear, close all clc x = 0:pi/36:2*pi y = cos(x) bar(x,y,'b')

Example

Example: Drawing a Stair-Step Plot clear, close all clc x = -10:0.5:10 y = x.^2 + 2.*x + 2 stairs(x,y,'b')

Example

Control Flow Operators Conditional statements Loops branching

Conditional Operators If…else Operator The if (condition)…else operator tests a condition. If the condition is true, then execute the if block. If the condition is false, execute the else block (or end without else). if (condition) % if block else % else block end if (condition) % if block end

Conditional Operators If…elseif…else Operator if (condition1)…elseif (condition2)…else operator If the condition1 is true, then execute the if block and end. If the condition1 is false, and the condition2 is true execute the ifelse block and end. If the condition1 and the condition2 is false, execute the else block and end. if (condition1) % if block elseif (condition2) % elseif block else %else block end

Conditions % conditions that can be tested % == : is equal to % ~= : is not equal to % > : larger than % >= : larger than or equal % <= : less than or equal % < : less than

Example clear, close all clc aaa = rand(1,100); bbb = 1:1:100 color = 1; if (color == 1) % if block figure, plot(bbb,aaa,':r'); else % else block figure, plot(bbb,aaa,'b'); end

Example clear, close all clc x = 3; if (x > 5) disp('The number is more than 5.') elseif (x == 5) disp('The number is equal to 5.') else disp('The number is less than 5.') end

Conditional Operators Switch/case/otherwise Operator Switch among several cases based on expression. switch switch_expression case case_expression statements case case_expression statements : otherwise statements end

Example clear, close all clc mynumber = input('Enter a number:'); switch mynumber case -1 disp('negative one'); case 0 disp('zero'); case {1, 5, 6} disp('positive one'); otherwise disp('other value'); end In MATLAB switch blocks, only the first matching case executes

For loop Used to repeat a set of statements multiple times. The for loop format is: for(start:increment:end) %for block end

Example: Display value inside for loop clear, close all clc for i = 1:1:5 st1 = strcat('The value of i inside the loop is: ',int2str(i)); disp(st1) end

Example: Check Value Inside Matrix clear, close all clc matA = [1.4,4.2,6.7,7.0; 5.5,6.7,8.9,3.0; 0.6,6.12,5.44,8.94] [row,col] = size(matA) for i = 1:1:row for j = 1:1:col currNo = matA(i,j); st1 = strcat('The value being tested is: ', num2str(currNo),'.'); disp(st1) if (currNo > 3) disp('The current value is larger than 3.') else disp('The current value is less or equal than 3.') end end end

While loop Used to repeat a set of statements while the tested condition is true. The while loop format is: while(condition)…. end The conditions are the same as if…else

Example: Display value inside for loop clear, close all clc counter = 1; while(counter <= 5) st1 = strcat('The value of i inside the loop is: ',int2str(counter)); disp(st1) counter = counter + 1; end

Complements of Loop break Terminate execution of for or while loop continue Pass control to next iteration of for or while loop pause Halt execution temporarily return Return to invoking function

2420-matlab_ruso_3_-_matlab_fundamentals.ppt
- Количество слайдов: 29