Sol#2
Transcription
Sol#2
EE4107 -‐ Cybernetics Advanced Exercise 2: Basic MathScript (Solutions) A major part of the course consists of practical implementation of the theory ("Learning by doing") in the form of practical/computational problem solving and project work. For this we will use the computer programs MathScript/LabVIEW. Both MathScript and LabVIEW are used in other courses. These tools are also widely used in industry and research today. MathScript and LabVIEW have built-‐ in support for control theory and simulation problems, which Visual Basic, Maple or other tools are missing. Engineers use various computer tools every day, so it is essential that you can use such tools! MathScript is a high-‐level, text-‐ based programming language. MathScript includes more than 800 built-‐in functions and the syntax is similar to MATLAB. You may also create custom-‐made m-‐file like you do in MATLAB. MathScript is an add-‐on module to LabVIEW but you don’t need to know LabVIEW programming in order to use MathScript. Note! Maple can do symbolic math, while MathScript (and VB/C#) is a numerical tool. This means you always have to define values for your variables before you can use them in expressions. How do you start using MathScript? You need to install LabVIEW and the LabVIEW MathScript RT Module. When necessary software is installed, start MathScript by open LabVIEW: When you open LabVIEW, select Tools -‐> MathScript Window...: Below we see the MathScript Window: Faculty of Technology, Postboks 203, Kjølnes ring 56, N-3901 Porsgrunn, Norway. Tel: +47 35 57 50 00 Fax: +47 35 57 54 01 2 Task 1: Basic Math Task 1.1 Given the following function: 𝑦 𝑥 = 4𝑥 + 2 𝑥 Find 𝑦 2 and 𝑦 5 using MathScript. Solution: We find 𝑦 2 : In order to do that, we type the following in the Command Window: First we need to define 𝑥 : x = 2 Then we can define the function: y = (4*x + 2)/x Then MathScript respond with the following answer: EE4107 -‐ Cybernetics Advanced 3 y = 5 We find 𝑦 5 : We define a new value for 𝑥 : x = 5 Then we execute the function once more: y = (4*x + 2)/x Then MathScript respond with the following answer: y = 4.4 Note! We always have to define values for our variables (𝑥) before we can use them in expressions. Task 1.2 Create the following mathematical expression in MathScript: 𝑧 = 3𝑥 ! + 𝑥 ! + 𝑦 ! + 𝑒 !" (!) 𝑧 is a function of 𝑥 and 𝑦, i.e. 𝑧(𝑥, 𝑦). Use MathScript to find 𝑧(2,2) (compare the answer using a simple calculator). Solution: MathScript code: x = 2; y = 2; z = 3*x^2 + sqrt(x^2 + y^2)+ exp(log(x)) MathScript gives the following answer: z = 16.8284 Note! We always have to define values for our variables (𝑥 and 𝑦) before we can use them in expressions. Task 2: Vectors and Matrices Task 2.1 Create the following vectors in MathScript: 4 𝑥 = 3 5 EE4107 -‐ Cybernetics Advanced 4 𝑦 = 1 ⋮ 10 Solution: MathScript code: x = [4 3 5] y = 1:10 Note! For creating the 𝑦 vector we have used the colon operator (“:”) in MathScript which is very handy. We will use this operator a lot in other tasks as well. Task 2.2 Create the following matrices in MathScript: 𝐴= 𝐶 = 0 −2 −1 4 1 1 −3 2 10 0 0 −2 6 Solution: MathScript code: A = [0 1; -2 -3] C = [-1 2 0; 4 10 -2; 1 0 6] Task 2.3 Find the solution (i.e. find 𝑥!, 𝑥! ) to the following equations using MathScript: 𝑥! + 2𝑥! = 5 3𝑥! + 4𝑥! = 6 Solution: We get: 𝐴𝑥 = 𝑏 1 3 2 𝑥! 5 = 4 𝑥! 6 Since 𝐴 in quadratic and we can find the solution like this: 𝑥 = 𝐴!! 𝑏 MathScript code: EE4107 -‐ Cybernetics Advanced 5 A = [1 2; 3 4]; b = [5;6]; x = inv(A)*b The following solution is displayed in the Output window: 𝑥! = −4, 𝑥! = 4.5 A more general approach would be to use the built-‐in backslash “\” operator to solve the equations: A = [1 2; 3 4]; b = [5;6]; x = A\b This approach is more robust, because it will work even when the 𝐴 matrix is not quadratic (meaning it is not invertible). Task 3: Plotting Task 3.1 Plot 𝑠𝑖𝑛(𝜃) for 0 ≤ 𝜃 ≤ 2𝜋 using MathScript. Plot 𝑐𝑜𝑠(𝜃) for 0 ≤ 𝜃 ≤ 2𝜋 using MathScript. Try to plot them in 2 different plots and then try to plot them in the same plot. Finally, try to plot them in 2 different subplots. Solution: Note! We cannot use Greek letters in MathScript, but use, e.g., “theta”, “x” or another name for your variable. Sinus: MathScript Code: x = 0:0.01:2*pi; plot(x, sin(x)) EE4107 -‐ Cybernetics Advanced 6 Cosinus: x = 0:0.01:2*pi; plot(x, cos(x)) Using the figure function: x = 0:0.01:2*pi; figure(1) plot(x, sin(x)) figure(2) EE4107 -‐ Cybernetics Advanced 7 plot(x, cos(x)) Using the hold command: In the same plot using the hold on command: x = 0:0.01:2*pi; plot(x, sin(x)) hold on plot(x, cos(x)) or like this: x = 0:0.01:2*pi; plot(x, sin(x), x, cos(x)) Colors: We can also use different colors: clf x = 0:0.01:2*pi; plot(x, sin(x), 'g') hold on plot(x, cos(x), 'r') Subplots: clf x = 0:0.01:2*pi; subplot(2,1,1) plot(x, sin(x), 'g') subplot(2,1,2) plot(x, cos(x), 'r') EE4107 -‐ Cybernetics Advanced 8 Task 3.2 Given the following differential equation: 𝑥 = 𝑎𝑥 ! where 𝑎 = − ,where 𝑇 is the time constant ! The solution for the differential equation is: 𝑥 𝑡 = 𝑒 !" 𝑥! Set 𝑇 = 5 and the initial condition 𝑥 (0) = 1 Plot the solution 𝑥 (𝑡) in the time interval 0 ≤ 𝑡 ≤ 25 Add Grid, and proper Title and Axis Labels to the plot. Solution: We define a script (m-‐file): %Define Variabes T=5; a=-1/T; %Start Condition, etc x0=1; t=[0:1:25] %Define the function x=exp(a*t)*x0; %Plotting EE4107 -‐ Cybernetics Advanced 9 plot(t,x); grid title('simulation of x'' = ax') xlabel('time') ylabel('x') The result becomes: Task 4: User defined functions Task 4.1 What is the room temperature in degrees Fahrenheit? Create a function in MathScript that convert a given temperature from degrees Celsius to degrees Fahrenheit. The conversion formula is as follows: 𝑇! = 9 𝑇 + 32 5 ! Solution: The function becomes (filename: fahrenheit.m): function Tf = fahrenheit(Tc) EE4107 -‐ Cybernetics Advanced 10 Tf = (9/5)*Tc + 32; We test the function (you can use the command window for this): Tc = 23 Tf = fahrenheit(Tc) This gives the following result: Tf = 73.4 Task 4.2 From mathematics we know that 2𝜋 = 360° Create two functions that convert from radians to degrees (r2d(x)) and from degrees to radians (d2r(x)) respectively. Test the functions to make sure that they work as expected, e.g.: >> r2d(2*pi) ans = 360 >> d2r(180) ans = 3.1416 Solution: It is quite easy to convert from radians to degrees or from degrees to radians. We have that: 2𝜋 𝑟𝑎𝑑𝑖𝑎𝑛𝑠 = 360 [𝑑𝑒𝑔𝑟𝑒𝑒𝑠] This gives: 𝑑 𝑑𝑒𝑔𝑟𝑒𝑒𝑠 = 𝑟 [𝑟𝑎𝑑𝑖𝑎𝑛𝑠] ∙ 180 𝜋 𝑟 𝑟𝑎𝑑𝑖𝑎𝑛𝑠 = 𝑑[𝑑𝑒𝑔𝑟𝑒𝑒𝑠] ∙ 𝜋 180 The functions are as follows (filename: r2d.m): function d = r2d(r) d = r*180/pi; and (filename: d2r.m): function r = d2r(d) EE4107 -‐ Cybernetics Advanced 11 r = d*pi/180; Testing the functions: >> r2d(2*pi) ans = 360 >> d2r(180) ans = 3.1416 Or: x = 0:0.1:360; x2 = d2r(x) y = sin(x2) plot(x,y) → From the tests, we see the functions are correct. Task 5: Flow Control MathScript have different flow control just like other programming languages. We will focus on the if-‐else statement and the for loop. Task 5.1 Depending on a variable 𝑎 , different functions should be executed and plotted according to: 𝑎 = 1 → 𝑦 = sin (𝑥) EE4107 -‐ Cybernetics Advanced 12 𝑎 = 2 → 𝑦 = cos(𝑥) 𝑎 = 3 → 𝑦 = tan (𝑥) 𝑒𝑙𝑠𝑒 → 𝑦 = 𝑥 Create a function that plot 𝑦 based on different values for the variable 𝑎 . The 𝑥 variable can be in the range 0 ≤ 𝜃 ≤ 2𝜋 Example of use: a=1; myplot(a) a=2; myplot(a) Here is “myplot” the name of the function you create. Solution: MathScript Code: function myplot(a) x = 0:0.1:2*pi; if a == 1 y=sin(x); elseif a == 2; y=cos(x); elseif a == 3 y=tan(x); else y=x; end plot(x,y) Task 5.2 The average/mean of a series of numbers 𝑥! , 𝑥! , ⋯ , 𝑥! is given by the following formula: EE4107 -‐ Cybernetics Advanced 13 𝑥! + 𝑥! + ⋯ + 𝑥! 1 𝜇=𝑥= = 𝑁 𝑁 ! 𝑥! !!! Create an average function that calculates the average of a vector with data. Use a for loop to iterate through the values in the vector and find the sum in each iteration, e.g.: function … … for … tot = tot + x(i); … Test the function in the Command window, e.g.: x = [2, 4, 5, 6] avg = average(x) This should give the following answer: avg = 4.25 Solution: Function (filename “average.m”): function avg = average(x) %This function calculates the average of a vector x N = length(x); tot=0; for i = 1:N tot = tot + x(i); end avg = tot/N; Calling the function: x = [2, 4, 5, 6] avg = average(x) This gives the following answer: avg = 4.25 Task 5.3 Combine the knowledge from task 5.1 and 5.2 and create a function that calculates your average grade so far. We can use the function like this: EE4107 -‐ Cybernetics Advanced 14 mygrades = ['A', 'B', 'B'] grade = avg_grade(grades) This gives the answer: grade = B Since you want to find the average, you need to convert the letters to numbers. Use the following conversions: A B C D E 5 4 3 2 1 Solution: The function can for example be written as: function avg = avg_grade(x) %This function calculates the average grade N = length(x); tot=0; for i=1:N if x(i)=='A' x(i)=5; elseif x(i)=='B' x(i)=4; elseif x(i)=='C' x(i)=3; elseif x(i)=='D' x(i)=2; elseif x(i)=='E' x(i)=1; else x(i)=='F' x(i)=0; end tot=tot+x(i); end avg = round(tot/N); if avg==5 avg='A'; elseif avg==4 avg='B'; elseif avg==3 avg='C'; elseif avg==2 EE4107 -‐ Cybernetics Advanced 15 avg='D' elseif avg==1 avg='E'; else avg==0 avg='F'; end Additional Tasks A1: Cylinder surface area Create a function that finds the surface area of a cylinder based on the height (ℎ) and the radius (𝑟) of the cylinder. We can use the function like this: h=8 r=3 cylindar_surface(h,r) Solution: We have that: EE4107 -‐ Cybernetics Advanced 16 The function becomes: function A = cylindar_surface(h,r) % This function calculates the surface of a sylindar A = 2*pi*r^2 + 2*pi*r*h; Testing the function: >> h=8 >> r=3 >> cylindar_surface(h,r) ans = 207.3451 A2: Pythagoras Pythagoras theorem is as follows: 𝑐 ! = 𝑎 ! + 𝑏 ! Create a function that uses Pythagoras to calculate the hypotenuse of a right-‐angled triangle, e.g.: function c = pythagoras(a,b) % … … c = … EE4107 -‐ Cybernetics Advanced 17 You can, e.g., use the function like this: a = 2 b = 3 c = pythagoras(a,b) Solution: The function may be written like this: function c = pythagoras(a,b) % This function calculates the hypotenuse of a right-angled triangle c = sqrt(a^2 + b^2); Testing the function in the Command window gives: >> pythagoras(2,3) ans = 3.6056 A3: Advanced expressions Create the following expression in MathScript: 𝑓 𝑥 = log 𝑎𝑥 ! + 𝑏𝑥 + 𝑐 − sin (𝑎𝑥 ! + 𝑏𝑥 + 𝑐) 4𝜋𝑥 ! + cos (𝑥 − 2)(𝑎𝑥 ! + 𝑏𝑥 + 𝑐) Given 𝑎 = 1, 𝑏 = 3, 𝑐 = 5 Find 𝑓 9 (The answer should be 𝑓 9 = 0.0044) Tip! You should split the expressions into different parts, such as: poly = 𝑎𝑥 ! + 𝑏𝑥 + 𝑐 num =… den =…. f =… This makes the expression simpler to read and understand, and you minimize the risk of making an error while typing the expression in MATLAB. Solution: The MathScript Script can be written like this: % Define the variables: a = 1; EE4107 -‐ Cybernetics Advanced 18 b = 3; c = 5; x = 9; % Split functions into parts for easy maintenance poly = a*x^2 + b*x + c; num = log(poly)-sin(poly); den = 4*pi*x^2 +cos(x-2)*poly; f = num/den Testing the Script: >> advexpression f = 0.0044 We can also create a function for this, which is probably a better solution. Additional Resources • http://home.hit.no/~hansha/?lab=mathscript Here you will find tutorials, additional exercises, etc. EE4107 -‐ Cybernetics Advanced