Download as pdf
Download as pdf
You are on page 1of 4
Numerical methods - Introduction to MATLAB IMPORTANT: In MATLAB there are some restrictions for the flenamesivariable names + cannot start wth numbers ‘+ avoid spacebarispecial characters and mathematical oparators For info about any MATLAB spectic questions help max X help function nane, or just search in the help guide you want to stop the process, hit: CTRL + ¢ ‘On any function, you can get info from the documentation by hiting FA ‘lear 9] X clean x = aeleter variable x fron the workspace % ‘yu can add comments to your script eh you ean open new section to your script 3 cancels echo TAB use itt fish the started functonnametvarable name ete [ARROW KEYS. you can scroll around the previously processed commands (you can access those from tne command history too) CTRL +ENTER run the whole section Fs run the selected part Fs run the whole sept MATLAB basics Difining variables: clear > X deletes variable ‘2 pb % built in variapte for pi 92) Kets 27 ‘The resus of your calculations are presenta til4 decimals by deaul, BUT: ths is JUST A REPRESENTATION! For calculations: use the variable, where you stored the calculated value! pL I you work wih matiicesivectors, be careful win te diferent parenthesis, because each type Is doing diferent operations: (), £1.) he23; 4561 2aty-3 noted size) X size of the matrox v= [1345 33 78] % rowector ongth() X length of # vector = [25 4; 22; 65; 28) colunn vector we 4 via transpose operator (") you can switch the rows and colums ‘You can access the elements of a vectorimatrix with index numbers tay second slew of actor ©) AEA tens) XE the 1ast eleneat of vector vi 78 ‘You can rewrite the value of each element withthe same way: Haya; p70 enpty vecton/natrax vOrt) X deletes the third clanent of vector v, therefor from this point: v= 12.33 78 ‘You can access multiple elements ofa vector/marix too: tea x result: 47 22 66 ‘You can create sequences easily via semicolon vas ae creates a list fron 10 20 = startnumbersencruber x3 = Linspace(1,10,10) creates a List fron 1 to 10, that has 18 elenents - Linspace(startnurber,endnunbernunber of elenents) ‘The vectors and matries could be combined together horzontalyvericaly easly - they have the same number of rows/columns: x» rand(z.3) generates 2 2-0y-3 matrix fale with rondo numbers in the énterval [8,1] Y= ones(2.4) generates a 2-oy-e matrix filled with ones z= ee) A generates a 3-vy-3 Adentity matrix ny = BW a 2-by-7 natrbx, bye combining X and ¥ horizontally x = 0% 2] a Seby-a matrix, bye comaining X and Z horizontally ‘Accessing one row/eolumn ina malic ny) 1 the First row of the matrix XY - via semicolon, you can access ALL elements »12) the first column of the matrix XY = via senicolon, you can access ALL elerents ya(:,end) the 1ast column of the matrix HY ~ via senicolon, you can access ALL elenents ‘Strings as vectors created fom characters str 'p string type variable, size 2-by-1 rn = ‘University of Technology’ string type variable, stze i-by-26 tne = ('sudspest * ne) you can conbine then together with brackets “{]* - Budapest University of Technology bne(s3:29) 4 the strings could be handlee and taken apart 25 well Plotting - the basics ay = load ‘betonace-tx0")5 Koy = [@, 0.2, 2, 20, 25; 0, 300, 285, 450, 250); xe yeas prot (x.y) plot(xys =) plot (xy, r=") ‘You can add many arguments to define the specifies ofa plot, tke the shape and size ofthe markers, specifies ofthe lines, plot (ny, 85's. Linen 92, arkertdgeolon by ‘arkerFaceColor’[@.540.5,0.5]) Useful specifiers: mm Seat 08 oe sve ower mee mens Dawraneningane co ‘set GES ioe Additional useful tips for plotting Each figure, andthe elements plotted on them could be named with handle (an identife). You can use this to acces any Agurellement ater, if you want to set a property, or clear them. Ifyou don't do further setings, by plating @ new element, the previous one willbe deleted. ‘lf lear figure f= Figures 1 the Figure function creates 2 new Figure pu = plottarys'o*) hold | 1 you con (3x the disgram, to sas new elements to 4€ without cleaning st flest 2 = plottny)s 3 = plot(xsys 009: detete(en) % you con delete elenerts by using its handle #igure(a) by referring to the nuber of the Figure, you can work on any previous Figure Functions in MATLAB he arguments of functions are always inside the regular parenthisis, and while you are typing MATLAB i constantly giving you hints what Kind ‘of addtional inputs can you add. Use the help guide anytime by pressing F1 onthe function! sintel) TE FasuLEt 0 (at uaat thin |u| Balada [on Post point rap uaenta thon er @] onto) this fe the e°8, which ts 1, e means the euler runner 2.7283, ‘The bull functions are also working on vectors: x= Lnspace(®,2%p 40) y= since) plot (xy 'bs-") ‘You can also write your very own functions, and you have more options. For the more simple functions you can create anonymous functions, that are not saved as a separate program: they re just assigned toa variable! For example, you want to create a function forthe following. fla) = 22-3001 ‘The MATLAB code willook tke this 09 2nd Se ‘This means the functions assigned tothe variable and thas a single argument. You define the input parameters wth the @ sign, and you an put multiple arguments inside the parenthesis if you wish, I you want to call the function, you can refer to its variable, as follows: (2.2045) his function works algo on vectors. nat only on single values! Is because we added a dot operator before we squared x. you want te make your own function to work sila, you should add 2" before mutpteaton,dvsion and in exponential expressions as flows Ifyou adaisubtract. o multiply your inputs by a seaar vale, this snot necessary Ifyou want to wrte separate function, you should apply the folowing structure in new sent fe: function foutputt, output2| = funclonnametinput, Input.) output = format output = formuiaz end Y= probated); plot(a,y2) About the furtons: + It should start with the Keyword function + It should have at east one input and one output + Inthe frst row you should have the output, the functions name, and the input the functions name should be the same as, them fle + Inside the function you should assign a vale to the output + The variables defined inside the functions are local variables, those wor't appear inthe workspace, also the running function has no ‘access tothe variables inside the workspace, on¥y if you added them, as an input ‘Your own function could have multiple inputs, just madly the previous function as fllows: function y = probafv2(xp) ye2mop- 8% +1 end ‘Save it 28a separate function, with ts name, and realli yt = probatva(at,2)5 plot(a.ya} ‘A function could have multiple outputs as wel defined as a vector function po x3, x4] = hatvany(x) end ‘Save this as a separate fle and lets present the results on a figure a2, 23, x4] = hatvany(eds plot (xy 492,433,338) ‘On the fgure you willsee each eloment with separate colors, but you can customize them an your own too: plot aye, black’ yxyx2,"blues4,43y "green! asx, red") ‘You can add addtional information too e. legend, title, axis label a follows: Legend x5 02's 03 4°4"y Location’, 'Southeast*) ‘stle( "Power fereeions") saabel 0 )5 Ylabel(‘Functions value") ‘eunction y = prosafvts) ye teed 3K 61; ‘emetion y = propafv2tise) yep 3+ ty ond Function (925 33, x4) = hatvany(x) Wx; ond 4 adding legend 4% adding lavels for the vertical axis

You might also like