Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
26 views
Unit Three Partb-1
This contains matrix
Uploaded by
bed-com-45-22
AI-enhanced title
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save Unit_Three_partB-1 For Later
Download
Save
Save Unit_Three_partB-1 For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
0 ratings
0% found this document useful (0 votes)
26 views
Unit Three Partb-1
This contains matrix
Uploaded by
bed-com-45-22
AI-enhanced title
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save Unit_Three_partB-1 For Later
Carousel Previous
Carousel Next
Save
Save Unit_Three_partB-1 For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 10
Search
Fullscreen
cO Functions By a functions we shall mean self-designed MATLAB script m-files which contain a few lines of codes meant for executing a certain process. For instance, writing a function file for solving a system of linear equations we have looked at briefly in Unit 2. Throughout our discussion in this lesson, when we are to consider writing algorithms can be classified into two types: direct and iterative methods. With the former, we obtain the solution in a finite number of steps, assuming. no rounding errors, for example to solve a linear system with Gaussian elimination. Iterative \n that converge to the methods involve the process involves generating a sequence of approxim: solution as the number of steps approaches infinity. 1, Writing function files A basic function files structured in such a way that it has an output array, function file and input array defined as Oe function [output}=function_name{input) aeaaesese'reewewses where input contains variable values defined by the user as to be used in the cade while function name is the title of the file name for the script. However, itis not always the case that we specify the output. For example we can have a structure either as | function foutput}=function_nametinput) Code end or function function_nametinput) code (output) end ‘Sometimes inputs are specified either inside the code or at the command window. As such, the structure of the MATLAB function file changes and appears as, 59function function_name finput) Main code (output) After the function declaration, say function [output]=function_name(input), in the m.file, it is possible to write a description of the function. This is done with the comment sign “%” before each fine. Then, from the command window, we can read this information about program's description when we type “help
”. MATLAB will automatically generate a display when commands are executed. However, in addition to this automatic display, MATLAB has several commands that can be used to generate displays or outputs. Two commands that are frequently used to generate output are: disp and printf. Example In order to compute the (N+1}-term Taylor series approximation to e* where e* = Sg, we write a Matlab script file based on the following pseudo code; Pseudo code 2xN>0 Inp Output: (IV + 1)-term Taylor series approximation to e* taylor=1; factorial=1; xpowk: for ke1 toN, do factorial = factorial * 2; xpowk=xpowk*x; taylor=taylor + (xpowk/factorial); end 60‘Thus, in Matlab, we implement the following code; function [taylor] =Taylorestimate (x,N) taylo! factorial=1; for k-1:N factorial=factorial*k: xpowk=xpowk*x; taylor=taylor+ (x ; end disp ("The (N+1) approx to evx is:') end With such a function file, to run, we type the main file line with inputs specified to get the results as sylorestimate(2,10) The (N+1)-term Taylor series approx to ex is: taylor= 7.3890 Example The following is a MATLAB fun function % FACTORIAL(N) returns the factorial of N % Compute a factorial value. rod(1:n); Such that for n = 5, we get on the command window >> f= factorial) fe | 120 612. Mathematical functions ‘A number of functions in MATLAB do not need to de defined or derived, however. For example, MATLAB already offers many predefined mathematical functions “called built-ins” for technical computing which contains a large set of mathematical functions. These functions can be accessed if we either type help elfun or help specfun in the command window to call up full lists of elementary or special functions, respectively. Table 8 lists some commonly used functions, where variables x and y can be numbers, vectors, or Toble 8: Elementary MATLAB built-in mathematical functions matrices. cosine sine tan(x) tangent acos(x) arch cosine asin(x) are sine atan(x) are tangent sqrt(x) square root Jog(x) natural logarithm Jogl0(x) standard logarithm Example To evaluate the expression y following statements in MATLAB; [oeanss >>x=2; >>ye8 >> y= expl-a)*sin(x)+10*sartly) vy 28,2904 Example abs(x) absolute value sign(x) signum function max(x)_ maximum value min(x) minimum value ceil(x) round towards +00 floor(x) round towards —o0 round(x) round to nearest integer rem(x) remainder after division angle(x) phase angle complex conjugate 4 sin(x) + 10/y, for a = 5,x = 2 andy = 8, we type the To evaluate the expression V = Inx + e!sin("/,) —logiox. >>xe7; >> Velog(x)+exp(10)}*sin{pi/4)-log10(x) ve 1.5576e+04 623. Plot functions The very basic plot function imbedded in MATLAB displays a graph of one’s interest in 2- dimensional. For example, the function plot can be used to produce a graph from two vectors x and y. Recall that a vector is an array of elements. This means by usage of vectors, we have two. related arrays of elements whose plot can produce a graph. During plotting, MATLAB is designed in such a way to plot one vector vs. another. The first one is treated as the abscissa (or x) vector and the second as the ordinate (or y) vector. These vectors have to be of the same length. Besides, MATLAB can as well plot a vector vs. its own index. The index is therefore treated as the abscissa vector. Given a vector “time” and a vector “dist” we could say: >> plot (time, dist) % plotting versus time >> plot (dist) % plotting versus index. For instance, the follo¥rng-evse———— >> plot(xy) produces a sine function graph whose x values range between —4 and 4, while y values range between —1 and 1 Other than going just by mere plotting function, we also have MATLAB commands to annotate the basic plot, by either adding axis labels, titles, and legends. These are defined by >> xlabel (X-axis label’) % puts a label on x-axis, ] >> ylabel (Y-axis uts 2 label on y-axis | ylabel ("Y-axis label’) % puts a label on y: a >> title (Title of my plot') % puts a title on the plot >> legend|'Key features of my plot") % highlights key features of the plotFor example, try running the following statements on the MATLAB command window and observe the differences in the outputs [> x=-pi0 Apis y=sin(y)s >> plot(xy) >> plot(x,y,'s-') >> xlabel('x'); ylabel(’ As another example, with a plot function file called aliasing, we can get Function aliasing xe =2:0.01:2; E_xesin(-2*x) 7 guxesin (64x); _xesin(-10°x) + plot (x, f_x)7 hold 0 plot (x/¢_x); hold off) end We can also add colors to the plotted lines for easy of distinguishing one line from the other in a multiple plot. For example>> x = O:pi/100:2* pi; >> yl =24c0s(x); >> y2-= cos(x); y3 = 0.5%c08(x); >> plot(x,yl.-1x,y2.X,93," >> xlabel(0 \leq x \leq 2\pi’) >> ylabel(Cosine functions’) >> legend('2*cos(x),'cos(x);'0.5*cos(x)’) >> title("Typical example of multiple plots’) >> axis({0 2*pi -3 3]) ‘The color of a single curve is, by default, blue, but other colors are possible. In a plot function, the desired color is indicated by a third argument. For example, red is selected by plot(xy,’r’). Note the single quotes, ° ’, around r. By default, MATLAB uses line style and color to distinguish the data sets plotted in the graph. However, we can change the appearance of these graphic components or add annotations to the graph to help explain our data for presentation. Some of the annotations are as presented in Table 9. Table 9: Attributes for plot Symbol [Color [Symbol [Line style [Symbol _| Marker k Black = [Solid + __| Plus sign r Red == [Dashed 0 _| Circle a Blue z | Dotted * | Asterisk Green =. _|Dash-dot ._| Point c Cyan none __| No line x__| Cross m. Magenta s__| Square Yellow d___| Diamond For example, it is possible to specify line styles, colors, and markers (e.g., circles, plus signs,...) using the plot command: where style_color_marker is a triplet of values from Table 9. For example by specifying asterisk and circle style markers to our “aliasing” funetion, the plot looks change to 65‘on aliasing 01:2; n(-2*x) 5 IDxesin (67x) 5 27g_xesin(~ plot (x, £_x, hold on plot (x,¢_x,'0"); hold © end Three-dimensional graphics can be produced using the imbedded functions surf, plot3 or mesh. For example, upon writing and executing the following meshgrid plot function in the command window >> [X,Y] = meshgrid{-10:0.25:10,-10:0.25:10}; >> f= sine(sqrt((X/pi).*2+(¥/pi).*2)); >> mesh(XV.f); >> axis([-10 10-10 10-03 1)) >> xlabel'{\bfx)) >> ylabel('\bfY') >> zlabel('{\bfsinc} ({\bfR))') >> hidden off we get an output As another example, with mesh plot function we can get 66>> A = zer0s(32); >> A(14:16,14:16) = ones(3); >> Feabs(fft2(A)): ° >> mesh(F) >> rotate3d on Comparably, with surf plot function we can get 35 A= zeros(32); >> A(14:16,14:16) = ones(3); >> Feabs(fft2(A)); >> surfllF) q >> rotate3d on . Activity 3 b a) Plot the graph of the funetion f(x) = cosx + sin mx for x € [—27, 2] with 0.1 sub- interval. b) Write a function that shows an approximation for a factorial can be found using Stirling’s formula: w= Rn) 67¢) Write a MATLAB function which should be able to find the volume and surface area ofa cylinder when it’s closed at both ends. 4)" Write a script file that calls a function to prompt the user and read in values for the hypotenuse and the angle (in radians), and then calls a function to calculate and return the lengths of sides a and b, and a function to print out all values in a nice sentence format. ¢) Write a for loop function to estimate V2 when t = [1,---,7] with reference to the recurrence equation for t = 1,23, yo Re Summary/Let Us Sum Up ‘To sum up, this unit was meant to introduce the student to the notion of counting, which is a key principle and very fundamental to most real life application problems. With principles of counting, techniques, the SUM RULE is noted to be useful for counting events that are made of collection of independent events. On the other hand, the COMBINATORIAL RULE gives us a chance to look at problems of making choices in life, which if we want to obtain such related unique choices, 68
You might also like
Using Matlab: Page 1 of 3 Spring Semester 2012
PDF
No ratings yet
Using Matlab: Page 1 of 3 Spring Semester 2012
3 pages
LAB1
PDF
No ratings yet
LAB1
6 pages
MainNumMath KMM
PDF
0% (1)
MainNumMath KMM
85 pages
Mathematical Functions: Polytechnic Institute of Tabaco
PDF
No ratings yet
Mathematical Functions: Polytechnic Institute of Tabaco
6 pages
Matlab Solver
PDF
No ratings yet
Matlab Solver
26 pages
Basic Plotting:: X (1 2 3 4 5 6) y (3 - 1 2 4 5 1) Plot (X, Y)
PDF
No ratings yet
Basic Plotting:: X (1 2 3 4 5 6) y (3 - 1 2 4 5 1) Plot (X, Y)
5 pages
MatLab and Solving Equations
PDF
No ratings yet
MatLab and Solving Equations
170 pages
Matlab Code PDF
PDF
100% (1)
Matlab Code PDF
26 pages
Matlab and Solving Equations
PDF
No ratings yet
Matlab and Solving Equations
26 pages
Matlab 3
PDF
No ratings yet
Matlab 3
33 pages
Lab # 1
PDF
No ratings yet
Lab # 1
10 pages
Matlab Intro
PDF
No ratings yet
Matlab Intro
19 pages
Software Toolkit: MATLAB
PDF
No ratings yet
Software Toolkit: MATLAB
15 pages
Computer Applications in Metallurgical Industries
PDF
No ratings yet
Computer Applications in Metallurgical Industries
52 pages
Quick Introduction To Matlab: Basic Data Analysis
PDF
No ratings yet
Quick Introduction To Matlab: Basic Data Analysis
10 pages
LAB ACTIVITY 1 - Introduction To MATLAB PART1
PDF
No ratings yet
LAB ACTIVITY 1 - Introduction To MATLAB PART1
19 pages
SS Lab Manual
PDF
No ratings yet
SS Lab Manual
48 pages
Matlablectures
PDF
No ratings yet
Matlablectures
13 pages
Khizran 3
PDF
No ratings yet
Khizran 3
9 pages
Introduction To Programming in MATLAB: Orhan Celiker
PDF
No ratings yet
Introduction To Programming in MATLAB: Orhan Celiker
47 pages
Lab Experiment 1 (A)
PDF
No ratings yet
Lab Experiment 1 (A)
14 pages
Matlab
PDF
No ratings yet
Matlab
3 pages
Document From ? 2
PDF
No ratings yet
Document From ? 2
33 pages
Simple Programming in MATLAB PDF
PDF
No ratings yet
Simple Programming in MATLAB PDF
11 pages
Intro M4 PDF
PDF
100% (1)
Intro M4 PDF
39 pages
Lab01_MATLAB Intro_ENGR2100U-1
PDF
No ratings yet
Lab01_MATLAB Intro_ENGR2100U-1
14 pages
DSP Report Akshat
PDF
No ratings yet
DSP Report Akshat
8 pages
Cours 4 Plotting in MATLAB (1)
PDF
No ratings yet
Cours 4 Plotting in MATLAB (1)
30 pages
EE 105: MATLAB As An Engineer's Problem Solving Tool
PDF
No ratings yet
EE 105: MATLAB As An Engineer's Problem Solving Tool
3 pages
Matlab Tutorials
PDF
100% (2)
Matlab Tutorials
13 pages
Matlab Vit
PDF
0% (1)
Matlab Vit
73 pages
MATLAB Reference Card
PDF
No ratings yet
MATLAB Reference Card
2 pages
Modeling & Simulation
PDF
No ratings yet
Modeling & Simulation
445 pages
Introduction To Matlab: SIN Sine of Argument in Radians. SIN (X) Is The Sine of The Elements of X. See Also ASIN, SIND
PDF
No ratings yet
Introduction To Matlab: SIN Sine of Argument in Radians. SIN (X) Is The Sine of The Elements of X. See Also ASIN, SIND
21 pages
Matlab Lab 2: Visualization and Programming
PDF
No ratings yet
Matlab Lab 2: Visualization and Programming
28 pages
Experiment 01
PDF
No ratings yet
Experiment 01
10 pages
Index: S.No Practical Date Sign
PDF
No ratings yet
Index: S.No Practical Date Sign
32 pages
Program No. 1: Table1.1: Matlab Window
PDF
No ratings yet
Program No. 1: Table1.1: Matlab Window
11 pages
Computational Lab (1)
PDF
No ratings yet
Computational Lab (1)
17 pages
Chapter 1n
PDF
No ratings yet
Chapter 1n
50 pages
LAB 1: Intro To Matlab: 1.1. Work Space Management
PDF
No ratings yet
LAB 1: Intro To Matlab: 1.1. Work Space Management
4 pages
Lab Report Matlab
PDF
No ratings yet
Lab Report Matlab
43 pages
Math3012 Numerical Analysis - Lab 2
PDF
No ratings yet
Math3012 Numerical Analysis - Lab 2
4 pages
Introduction To Matlab and Manipulation of Functions
PDF
No ratings yet
Introduction To Matlab and Manipulation of Functions
65 pages
PCS Lab-1
PDF
No ratings yet
PCS Lab-1
9 pages
SS Lab Manual
PDF
No ratings yet
SS Lab Manual
45 pages
Numerical Analysis Lab 1
PDF
No ratings yet
Numerical Analysis Lab 1
2 pages
Chapter 1
PDF
No ratings yet
Chapter 1
48 pages
MATLAB Unit 2 Study Material
PDF
No ratings yet
MATLAB Unit 2 Study Material
37 pages
Lab 1-Introduction To Matlab
PDF
No ratings yet
Lab 1-Introduction To Matlab
8 pages
Lecture Digital Image Processing 2017: Getting Started
PDF
No ratings yet
Lecture Digital Image Processing 2017: Getting Started
9 pages
Differential Equations (92.236) Listing of Matlab Lab Exercises
PDF
No ratings yet
Differential Equations (92.236) Listing of Matlab Lab Exercises
18 pages
Intro Tom at Lab
PDF
No ratings yet
Intro Tom at Lab
19 pages
Matlab Notes 2
PDF
No ratings yet
Matlab Notes 2
36 pages
Digital Communication Lab-03
PDF
No ratings yet
Digital Communication Lab-03
7 pages
Chapter 5_Plotting
PDF
No ratings yet
Chapter 5_Plotting
33 pages