EE211 | Introduction to MATLAB EE211 MATLAB CODE BASICS – AN OVERVIEW Basic Math Function Function Add Subtract Multiply Divide Absolute Value Command + * / abs(number) Notes Cosine Sine Tangent Inverse Cosine Inverse Sine cos(angle) sin(angle) tan(angle) acos(angle) asin(angle) angle must be in radians angle must be in radians angle must be in radians angle must be in radians angle must be in radians x=cos(0.5) x=sin(0.5) x=tan(0.5) x=acos(0.5) x=asin(0.5) Powers Square root Exponential x^y sqrt(number) exp(number) x raised to the y power will return imaginary if number<0 returns e^(number) x=theta^2 x=sqrt(theta) x=exp(theta) Natural log Log base 10 Log base 2 log(number) log10(number) log2(number) * and / take precedence over + and - Example x=a+b x=a-b x=a*b x=a/b x=abs(y) x=log(0.5) x=log10(0.5) x=log2(0.5) Logical Operators Operator == ~= < > <= >= and(A,B) or(A,B) Logic is equal to is NOT equal to is less than is greater than is less than or equal to is greater than or equal to True if A and B are true True if A or B is true 1 EE211 | Introduction to MATLAB Create Symbolic Functions Use syms to create symbolic variables. Then use symfun to create a symbolic function with these variables as its input arguments. Eg. syms x y f = symfun(x + y, [x y]) --f(x, y) = x + y --f(1,2) --ans = 3 Or use syms x y f(x,y) = x + y For Vectors or matrices: A = eye(3); A(1,1) = sym('pi') A = 3.1416 0 0 0 1.0000 0 0 0 1.0000 Using Symbolic Parameter Functions for sinusoids EXAMPLE: Using symbolic variable declaration syms to evaluate sinusoids. Consider two sinusoids of frequency 50Hz. v1 120cos(100 t 15); v1 240sin(100 t 30) Find vt v1 v2 ; We can see that the value for omega ( ) is same for both. However, one is a sine and the other is cosine function. Nonetheless, MATLAB provides an easy method to add two functions using symbolic operators. >> >> >> >> syms V1 V2 fi1 fi2 w t v1=V1*cos(w*t+fi1); v2=V2*cos(w*t+fi2); vt=v1+v2 2 EE211 | Introduction to MATLAB vt = V1*cos(fi1 + t*w) + V2*cos(fi2 + t*w) Notice that in the “Workspace” window, MATLAB has produced placeholders for all the variables that we have declared symbolically (i.e V1, V2, fi1, fi2, w, t) and evaluated (i.e v1, v2, vt). However, the foregoing expression is not of much use. We need to evaluate and graph those two functions and their sum. Let’s first assign the parameters with values. >> >> >> >> >> >> >> >> >> >> >> syms V1 V2 fi1 fi2 w t V1=120; V2=240; fi1=15*pi/180; fi2=30*pi/180; w=2*50*pi; t=0:1e-4:0.02; v1=V1*cos(w*t+fi1); v2=V2*sin(w*t+fi2); vt=v1+v2; plot(t,vt,t,v1,t,v2),grid Few things to note: When we end each line of code with a semicolon “;”, MATLAB does not return the value of the parameter to the command window. Makes the code more readable. However, you can see the Workspace data has changed. The following command: t=0:1e-4:0.02; sets an interval for the time domain (x-axis) from 0 to 1/50 (i.e. one full period of the waveform of a standard sinusoid without a phase shift i.e. v Vm cos(t ) ). The plot function in MATLAB is smart enough to recognize that you are adding 3 different waveforms each having its own x- and y-axis parameter. The grid function can be written in separate line or in the same with the comma. Sometimes we write grid on and grid off. This function simply draws grid in the plot window as the name implies. See Figure 1 for plot output of v1, v2 and vt. Hence MATLAB is built for engineers and mathematicians to intuitively use it to solve any mathematical parameter as they would do in manual calculation. 3 EE211 | Introduction to MATLAB Figure 1 In the Plot window: You can go on Tools menu and select Edit Plot. And click on any of the waveform to edit its properties as shown in Figure 2. Use help plot command to access more details on how to make your graphs more presentable. Figure 2 4