COMPUTER GRAPHICS WITH MATLAB COMPUTER GRAPHICS MATLAB MATrix LABoratory •Designed for matrix manipulations and operations •Numerical computing environment •Plotting of functions and data •Algorithm implementation •User interfaces •Rapid prototyping, with toolboxes WHY MATLAB? •Ease of use •Platform independence •Predefined functions •Device-independent plotting •Graphical user interface •MATLAB compiler MATLAB BASICS Fundamental unit of data in MATLAB is array • Array–collection of data values organized into rows and columns MATLAB environment • • • • • • • • Command window Command history window Edit/Debug window Figure window Workspace browser/Variable editor Help browser Current directory browser Work directory MATLAB PLOTTING • MATLAB has extensive device-independent plotting capabilities • Very simple and easy to plot data x=0:1:10; y=x.^2-10.*x+15; plot(x,y); GETTING ACCUSTOMED TO MATLAB Open matlab and see following features on matlab desktop Current folder : access your files Command window: Enter commands at the command line indicated by prompt (>>) WorkSpace: explore data that you create or export files from Command History: view or rerun commands GETTING STARTED WITH MATLAB Type clc in command window, now matlab is ready to be used Create variables; a=1 b= 2 c= a+b d= sin(b) e= a*b; GETTING STARTED WITH MATLAB Semicolon (;) suppress the display of output in command window Do not use same variable symbol twice in the same matlab session! Use clear command to clear workspace variables GETTING STARTED WITH MATLAB Array Creation: f=[1 2 3 4] Use space or comma (,) to separate elements of an array • to create a matrix having multiple rows and columns use semicolon (;) g=[1 2 3; 4 5 6; 7 8 9] Create a matrix having 5 rows and 6 columns? GETTING STARTED WITH MATLAB Try these commands h= g+10 g*5 Sin(g) To transpose a matrix use single quote (‘) e.g. g’ GETTING STARTED WITH MATLAB Complex numbers in matlab Complex numbers have both real and imaginary part sqrt(-1) ans= 0.0000 + 1.0000i To represent imaginary part of the complex number, use either I or j m= [3+4i 1+2j; -5i 7+8j] GETTING STARTED WITH MATLAB Use whos to check workspace variables w h o s GETTING STARTED WITH MATLAB Character strings mytext= ‘Hello World’ To c o n v e r t n u m e r i c v a l u e s t o s t r i n g s u s e n u m 2 s t r or int2str u=71; v= (n-32)/1.8; t e m p t e x t = [ ‘ Te m p e r a t u r e i s ’ n u m 2 s t r ( v ) , ‘ v ’ ] GETTING STARTED WITH MATLAB GETTING STARTED WITH MATLAB Hint* Assign variables and compute these terms GETTING STARTED WITH MATLAB 2D and 3D plots: Line Function: To create two dimensional graph use plot function. Use label and title GETTING STARTED WITH MATLAB LEGENDS xlabel(‘x’) ylabel(‘sin(y)’) title(‘graph of sine function’) LINE STYLE, MARKER STYLE, COLOR Using a third input argument, plot function with red dash line type. plot(x,y, ’r - -’) The ‘ r- -’ string is a line specification. Each specification can include characters for the line color, and marker style. A marker style that appears at each plotted data point such as a +, o, * For example, ‘g:* ’ requests a dotted plot with * markers GETTING STARTED WITH MATLAB ADD PLOTS ON AN EXISTING GRAPH Use hold on command To plot legends on the same Graph. GETTING STARTED WITH MATLAB PLOTTING - MULTIPLE PLOTS PLOTTING - MULTIPLE PLOTS The first two inputs to the subplot indicates the number of rows and columns and the third input specifies which plot is active GETTING STARTED WITH MATLAB 3D plots: Three dimensional plots typically display a surface defined by a function in two variables, Z= f(x,y) To evaluate z, first creat a set of (x,y) points over the domain of the function using meshgrid GETTING STARTED WITH MATLAB [x,y] = meshgrid(-2: .2: 2); Z= x.*exp(-x.^2 – y.^2); Surf(x,y,z) GETTING STARTED WITH MATLAB [x,y] = cylinder(-2: .2: 2); Z= x.*exp(-x.^2 – y.^2); Surf(x,y,z) GETTING STARTED WITH MATLAB GETTING STARTED WITH MATLAB GETTING STARTED WITH MATLAB GETTING STARTED WITH MATLAB GETTING STARTED WITH MATLAB GETTING STARTED WITH MATLAB GETTING STARTED WITH MATLAB GETTING STARTED WITH MATLAB