Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Matlab e Simulik

Similar presentations


Presentation on theme: "Introduction to Matlab e Simulik"— Presentation transcript:

1 Introduction to Matlab e Simulik
Ing. Barbara Rinauro, PhD student

2 What is Matlab Matrix Laboratory is a high-performance language for technical computing, for handling the calculations involved in scientific and engineering problems It integrates computation, visualization, and programming environment It has sophisticated data structures, contains built-in editing and debugging tools, and supports object-oriented programming It has powerful built-in routines that enable a very wide variety of computations (Functions that are frequently used or that can take more time to execute are often implemented as executable files) It is an interactive system whose basic data element is an array that does not require dimensioning It also has easy to use graphics commands that make the visualization of results immediately available Specific applications are collected in packages referred to as toolbox (libraries – statistics, text, optimization, curve fitting, symbolic)

3 2D Graphic results

4 3D Graphic results

5 Software and installation
MATLAB is product from Mathworks whos site is: How to download the software with university license o Installation instructions License 1 year validity

6 Matlab Desktop

7 M-file To create a new file click on «New» or «New script» for a script file To open and existing file, on which corrections or changes can be made, click on «Open» or type: >> edit nome-file.m Rules for naming M-file: Has to start with a letter Can contain only letters, numbers and underscore Cannot contain space Right: name.m ; Name1.m ; Name_1.m Wrong: 1name.m ; name 1.m ;

8 The only file that can be “run” is the script.
M-file Commands are written in the command window, for example: Value to a variable Expressions Function execution Graphs generation The commands entered in the Command Window cannot be saved and executed again for several times. Therefore, a different way of executing repeatedly commands with MATLAB is to create a file with a list of commands. There are 2 types of M-file: Script is an external file that contains a sequence of MATLAB statements and simply execute them. It has a filename extension .m. Functions are programs (or routines) that accept input arguments and return output arguments. Each M-file function (or function or M-file for short) has its own area of workspace, separated from the MATLAB base workspace. The only file that can be “run” is the script.

9 M-file From home window <<New>> script or function gives example

10 Managing workspace and file commands

11 Managing workspace and file commands
>> dir - Display directory listing >> what – List MATLAB files in current directory >> cd – Change current directory >> pwd – Identify current directory >> type nomefile – Display contents of file >> help Display help for MATLAB functions Tip: Case Sensitive (m-lower case different from M-uppercase)

12 Variable and Expressions
All information in matlab are called variables. MATLAB variables are created with an assignment statement >> variable name = a value (or an expression) So a variable has two characteristicts: name. value. The name for the variable can be choosen with a maximum length of 63 characters (namelengthmax) The name has to start with a letter and can only conatin letters, numebrs and underscors. An expression is a combination of numerical values, mathematical operators, variables, and function calls. On other words, expression can involve: manual entry built-in functions user-defined functions (ex_1=(a+b)/2, ex_2=ba, ...). Once a variable has been created, it can be reassigned.

13 Input The assignment of a value to a variable can be done in three ways. 1. The variable is defined in the script file. 2. The variable is defined in the command prompt (>>) in the Command Window 3. The variable is entered when the script is executed. Case 3: in script file command input and executing the file the user is prompted to assign a value to the variable in the command: a=input('enter the value of a = ') >> enter the value of a = Import Data Command: for file xls or txt Command xlsread >> d=xlsread('TEST_04_05')

14 Output MATLAB automatically generates a display when commands are executed. Results are not displayed by putting a semicolon (;) at the end of the line. Two commands frequently used to generate output are disp and fprintf display('My name is Barbara') >> My name is Barbara

15 Elementary instructions
Matlab as calculator >> 2+3 ans = 5 Assigning value 3 to variable a: >> a = 3 a = 3 Expression >> b=a* >> b=a*2; b = 6 Tip: ; - semicolon after expression does not display variable in command window

16 File commands who display variables currently in the workspace Il
whos Display information on variables in the workspace Clear (vars) Removes all variables from the workspace Clear x Remove x from the workspace clc Clear the Command Window

17 Predefined variables and math constants
ans: Value of last variable (answer); eps: Floating-point relative accuracy; pi: The number π ( ); i, j: Imaginary unit of a complex number −1 ; inf, -inf : Infinity (∞); NaN: “Not-a-Number” for indefined arithmetical operaions ex: 0/0) ; realmin: smallest real number than can be used(≈ 2.2E-308); realmax: biggest real number than can be used(≈ 1.8E308);

18 Format Options MATLAB by default displays only 4 decimals in the result of the calculations, for example − MATLAB does numerical calculations in double precision, which is 15 digits. The command format controls how the results of computations are displayed. To control exactly what your output looks like the commands to use are: sprintf and fprintf >> x= >>fprintf ('questo è il valore %4.2f', x)

19 Format Options

20 Arrays Matrices are the basic elements of the MATLAB environment. A matrix is a two-dimensional array: the first dimemsion represents the number of rows m and the second the number of colums n. A scalar is an array of dimension 1x1 An array of dimension 1×n is called a row vector An array of dimension m×1 is called a column vector A string is actually an array with each element representing one character in the string. Strings may be assigned to variables by enclosing them in apostrophes.

21 Matrix generation Empty Vector x=[ ] Row Vector - Matrix 1 x 3
The elements of vectors are enclosed by square brackets and are separated by spaces or by commas Column Vector - Matrix 3 x 1 Semicolon separate the components of a column vector,. Direct generation - Matrix 3 x 3 x=[ ] >> x = [ ] oppure x = [1, 2, 3] x = >> x = [1 ; 2 ; 3] x = 1 2 3 >> x = [1 2 3; 4 5 6; 7 8 9] x =

22 Vectors and matrice Indexing
x(i) is the element in position i of vector x. X(i,j) is the element in the row i and column j of the matrix X. To access blocks of elements, we use MATLAB’s colon operator (:) which stands for all columns or all rows >> A = [1 2 3; 4 5 6; 7 8 9] A = A (2,3) = 6 A(2,:) = A(:,3) =

23 Generation Matrices Colon operator
Entering a vector x consisting of points with fixed interval or interval=1: x = [first:interval:last] [ ] parenthesis are optional x = [first:last] Default interval equals to 1 >> x = 0:0.5:3 x = >> x = 0:3 x =

24 Generation Matrices Linear spacing Generating linearly spaced vectors
y = linspace(a,b) generates a row vector x of 100 points linearly spaced between and including a and b. x = linspace(a,b,n) generates a row vector y of n points linearly spaced between and including a and b. >> x = linspace(1,2) x = … 2 >> y = linspace(1,2,5) y =

25 Elementary matrices and arrays

26 Operation with Matrices
Sub-matrix A = B = A ( [ 2 3] , [1 2] ) B = C = A ( [2 1 3] , :) C = Traspose A row vector is converted to a column vector using the transpose operator. The transpose operation is denoted by an apostrophe or a single quote (’). >>a= 1 2 3 >>b = a’ = 1 2 3

27 Operation with Matrices
Deleting row or column A = A (3 , : ) = [] A = A = [A(1,:);A(2,:);[7 8 9]] To delete a row or column of a matrix, use the empty vector operator, [ ]. To restore the third row, we use a technique for creating a matrix

28 Arrays and Matrices: Basic information
Dimension of matrix size(variabile) Length of vector length(variabile)

29 Arrays and Matrices: operations and manipulation

30 Exercise 1: Given a ship with following dimensions L=90 B=13.5 T= 3.6
CB=0.5 calculate ship displacement. Answer Displ =

31 Exercise 2: Create a matrix A 2 x 3 with zeros in the first row and numbers 1,2,3 in the second row. Create from matrix A a Matrix B 2 x 2 with the last 2 columns of A. What is the position of number 2 in matrix B (with index)? Answer: B(2,1)

32 Exercise 3: Create a vector X of 9 elements, with the first one equal to 0 and the last equal to 16. Create a matrix A 3 x 3 by columns from vector X. Delete the first column of matrix A. What is the position of number 14 in matrix A? Answer: A(2,2)

33 Array operations Arithmetic operators Relation operators
+ Addition (plus) - Subtraction (minus) * Multiplication (times) / Division (by) ^ Exponentiation (power) Relation operators > greater than < less than >= greater or equal to == equal to ~= negation Logic Operators & and | or

34 Matrix arithmetic operations
A+B or B+A is valid if A and B are of the same size A*B is valid if A’s number of column equals B’s number of rows A^2 is valid if A is square and equals A*A α*A or A*α multiplies each element of A by α >> A=[1 2 3; 1 2 3] A = >> B=[2 2 2;3 3 3] B = >> C=A+B C = >> A=[1 2 3; 1 2 3] A = D=[1 2;2 3;3 4] D = >> E=A*D E = >> 0.5*E ans =

35 Array arithmetic operations
Array arithmetic operations are done element-by-element. The period character, ., distinguishes the array operations from the matrix operations. For addition (+) and subtraction (−), the character pairs (.+) and (.−) are not used, since the matrix and array operations are the same. .* Element-by-element multiplication ./ Element-by-element division .^ Element-by-element exponentiation

36 Array arithmetic operations
Difference between operation * (moltiplication by “dot product" of rows and columns) and .* (moltiplication element by element) A=[1 1;2 2] A = >> B=[2 2;3 3] B = >> C=A*B C = >> D=A.*B D =

37 Matrix functions There is a long list of mathematical functions that are built into MATLAB. These functions are called built-ins.

38 Elementary matematical functions

39 Example calculations >> log(142) ans = >> log10(142) ans = >> exp(10) ans = e+004 >> sin(pi/4) ans = Sin function needs values in radiants deg2rad(D) - Convert angle from degrees to radians

40 Exercise 4: Create a velocity vector composed of 9 velocities, beginning with 1 knot and finishing with 5 knots. Create a ship length vector composed of 5 lengths, beginning with 100 m and ending with 200 m. Calculate a Froude matrix containing every combination of Velocities and ship speed.

41 Solving linear equations
With matrix notation, a system of simultaneous linear equations is written as: 𝑨𝒙 = 𝒃 A is a given square matrix of order n, b is a given column vector of n components, x is an unknown column vector of n components. The solution of this equation can be written as: 𝒙 = 𝑨 −𝟏 ∗𝒃, where 𝑨 −𝟏 is the inverse of A.

42 Example solution System of equations
System of simultaneous linear equations 𝑥 + 2𝑦 + 3𝑧 = 1 4𝑥 + 5𝑦 + 6𝑧 = 1 7𝑥 + 8𝑦 = 1 𝑨𝒙 = 𝒃 A = 𝑏= Solution 𝒙 = 𝒊𝒏𝒗(𝑨)∗𝒃

43 Basic plotting – 2D plots
MATLAB has an excellent set of graphic tools. Given 2 vectors x-coordinates, x = (x1,...,xN), and y-coordinates, y = (y1,...,yN), where x and y are both row arrays or column arrays of the same length, to plot a graph you can: - use the command plot(x,y) - or go in the window PLOTS. The plot functions has different forms depending on the input arguments. If y is a vector plot(y) produces a piecewise linear graph of the elements of y versus the index of the elements of y. If we specify two vectors, as mentioned above, plot(x,y) produces a graph of y versus x. Note: x and y must be in an identical array form, same length.

44 Basic plotting – 2D plots
x=linspace(0,2*pi); y=sin(x); z=cos(x); plot(x) plot(y) plot(z)

45 Basic plotting – 2D plots
x=linspace(0,2*pi); y=sin(x); z=cos(x); plot(x,y) plot(x,z)

46 Basic plotting – 2D plots
Multiple graphs with a single call to plot plot(x,y,x,z)

47 Basic plotting – Titles and axis labels
Title : title(‘title’) Axis labels: xlabel(’x’) ylabel(’y’) xlabel('x') ylabel('sin x') title('Function sin x')

48 Basic plotting – Legend
Add legend to axes with commmand: legend(‘label 1’,’label 2’) plot(x,y,x,z) xlabel('x') ylabel(‘sin and cos x') title('Function sin and cos x') legend('sin x','cons x’) Add title to legend: lgd=legend('sin x','cons x’) title(lgd, 'Legend’) FontSize and TextColor properties >> lgd = legend ({'sin x','cons x'},'FontSize',12,'TextColor','blue')

49 Basic plotting – Legend
Legend location with respect to the axes, as listed in this table.

50 Basic plotting – style_color_marker
It is possible to specify line styles, colors, and markers (e.g., circles, plus signs) using the plot command: plot(x,y,’style_color_marker’) Line style, specified as one of the options listed in this table:

51 Basic plotting – 2D plots
Line color, specified as an RGB triplet or one of the color options listed in this table:

52 Basic plotting – 2D plots
Marker symbol, specified as one of the markers in this table:

53 Basic plotting – Edit plot
Modifing graphs directly from figure window: View  Property editor  Plot Browser Tools  Edit plot Insert  X label, Ylabel Legend

54 Basic plotting – Other commands for Plots
Display or hide axes grid lines grid on grid off grid minor  visibility of minor grid lines Retain current plot when adding new plots hold on hold off

55 Basic plotting – Text in plots
Add text descriptions to data points described by x and y. text(x,y,text) text(pi,0, '\leftarrow sin(\pi)', 'FontSize',14,'Color','red’) To include numeric variables in the text, use the num2str function v = 42; text = ['The value is ',num2str(v)]; 'HorizontalAlignment' of text with respect to position point ‘left’ (default) ‘center’ ‘right’ text(pi,0 , 'sin(\pi) \rightarrow ', 'HorizontalAlignment','right')

56 Basic plotting – Text in plots
This table lists supports special characters:

57 Basic plotting – Subplots
Create axes in tiled position subplot(m,n,p) divides the current figure into an m-by-n grid and creates axes in the position specified by p subplot(1,2,1) plot(x,y) xlabel('x') ylabel('sin x') title('Function sin x’) subplot(1,2,2) plot(x,z) ylabel('cos x') title('Function cos x')

58 Basic plotting – 3D plots
To Plot a 3D graph the commands are various: With vecotors: plot3(x,y,z) t = 0:pi/50:10*pi; st = sin(t); ct = cos(t); plot3(st,ct,t)

59 Basic plotting – 3D plots
To create a three-dimensional surface plot use the command surf(x,y,z). The function plots the values in matrix Z as heights above a grid in the x-y plane defined by X and Y [X,Y] = meshgrid(1:0.5:10,1:20); Z = sin(X) + cos(Y); surf(X,Y,Z) Z must be a matrix. The command [X,Y] = meshgrid(x,y) returns 2-D grid coordinates based on the coordinates contained in vectors x ans y x = 1:3; y = 1:5; [X,Y] = meshgrid(x,y)

60 Control flow and operator
MATLAB is also a programming language and has some decision making structures for control of command execution. These decision making or tools that can be used to control the flow of a program include: for loops while loops if-else-end constructions

61 Control flow and operator - If, elseif, else
If,elseif, else - executes statements if condition is true. The syntax is: if expression statements elseif expression else End Note: elseif has no space between else and if (one word) no semicolon (;) is needed at the end of lines containing if, else, end indentation of if block is not required, but facilitate the reading. the end statement is required

62 Control flow and operator - Relational and logical operators
A relational operator compares two numbers by determining whether a comparison is true or false

63 Control flow and operator - if
Example x = 10; minVal = 2; maxVal = 6; if (x >= minVal) && (x <= maxVal) disp('Value within specified range.') elseif (x > maxVal) disp('Value exceeds maximum value.') else disp('Value is below minimum value.') end

64 Control flow and operator - For
In the for ... end loop, the execution of a command is repeated at a fixed and predetermined number of times. The syntax is: for variable = expression statements end expression is normally equal to start:step:stop x=[ ] for i = 1:length(x) y(i)=2.*x(i); y=y

65 Control flow and operator - For
m=4; n=5; for i = 1:m for j = 1:n H(i,j) = 1/(i+j); end H=H >>H =

66 Control flow and operator - While
The while … end loop is used when the number of passes is not specified. The looping continues until a stated condition is satisfied. The while loop has the form: while expression statements end x = 1 while x <= 10 x = 3*x

67 Exercise 4: x is a vector of 21 values from 0 to 10. Plot the following two functions like in the following 2 figures: y=2+3*x-5*x.^2 z=5-4*x+x.^2

68 Exercise 5: Create a script that displays the sum of the numbers that go from 1 to 5. Sum = 15 Exercise 6: From v=[ ] display in command window each position of element of vector v and if it’s value is less or grater than 5. Example of solution:  v =    element's value in position 1 is less than 5 element's value in position 2 is less than 5 element's value in position 3 is greater than 5 element's value in position 4 is greater than 5 element's value in position 5 is less than 5 element's value in position 6 is greater than 5 element's value in position 7 is less than 5

69 Exercise 7: Considering a ship of length L = 90 m and a sea density of 1025 kg/m^3, import the excel file called sectiondata. Given series of wave with the following characteristics: wave steepness, sj = Hij/λi, varing from 0.03 to 0.15 with increment of Δs = wavelength to ship length ratio, ri = λi/L, varing from 1.0 to 3.0 with increment of Δr = 0.025 calculate the Surging forces with formula:   𝑓 𝑖𝑗 =𝜌𝑔 𝑘 𝑖 𝐻 2 𝐹𝑐 𝑖 2 + 𝐹𝑠 𝑖 2 function of the Froude Krylov forces given by the following formulas 𝐹𝑐 𝑖 = 𝑚=1 𝑁 ∆ 𝑥 𝑚 𝑆 𝑥 𝑚 𝑠𝑖𝑛 𝑘 𝑖 𝑥 𝑚 exp(−0.5 𝑘 𝑖 𝑑 𝑥 𝑚 ) 𝐹𝑠 𝑖 = 𝑚=1 𝑁 ∆ 𝑥 𝑚 𝑆 𝑥 𝑚 𝑐𝑜𝑠 𝑘 𝑖 𝑥 𝑚 𝑒𝑥𝑝(−0.5 𝑘 𝑖 𝑑 𝑥 𝑚 ) Where: 𝑘 𝑖 = 2𝜋 𝜆 𝑖 is the wave celerity 𝐻 𝑖𝑗 = 𝑠 𝑗 𝑟 𝑖 𝐿 is the wave height 𝑥 𝑚 is the longitudinal distance from the center of ship mass to a m-th station (m), positive for a bow section; Partial solution: Fij(1,1) = e+05

70 Polynomials The command polyval is used to calculate the value of a polynomial y = polyval(p,x) returns the value of a polynomial of degree n evaluated at the value x. The input argument p is a vector of length n+1 whose elements are the coefficients in descending powers of the polynomial to be evaluated. A coefficient of 0 indicates an intermediate power that is not present in the equation. 𝑝=2 𝑥 4 +3 𝑥 2 +4 p = [ ]; y = polyval(p,1) = 9 𝑝=2 ∗ ∗1 2 +4=9 y = polyval(p,2) = 48 𝑝=2 ∗ ∗2 2 +4=48 y = polyval(p,[2 3]) = 𝑝=2 ∗ ∗3 2 +4=193

71 Polynomials The command polyval is used to evaluate polynomial differentiation k=polyder(p) returns the derivate of the polynomial represented by the coefficients in p. 𝑘 𝑥 = 𝑑𝑝(𝑥) 𝑑𝑥 t=polyder(a,b) returns the derivate of the product of the polynomials a and b. 𝑘 𝑥 = 𝑑[𝑎 𝑥 ∗𝑏 𝑥 ] 𝑑𝑥 [q,r]=polyder(a,b) returns the derivate of the quotient of the polynomials a and b. 𝑞(𝑥) 𝑟(𝑥) = 𝑑 𝑑𝑥 𝑎(𝑥) 𝑏(𝑥)

72 Polynomials 𝑝= 𝑥 3 −3 𝑥 2 +5 𝑎= 3𝑥 2 +4𝑥 𝑏= 2𝑥 3 −3 𝑎∗𝑏= 6𝑥 2 +4𝑥−2 p = [ ]; a = [3 4 0]; b = [ ]; k = polyder(p) = 𝑑𝑝 𝑑𝑥 = 3𝑥 3 − 3∗2 𝑥+0 t = polyder(a,b) = 𝑑𝑎∗𝑏 𝑑𝑥 = 30𝑥 𝑥 3 −18x−21 [q,r] = polyder(a,b) = 𝑑 𝑑𝑥 𝑎 𝑏 = −6 𝑥 4 −16 𝑥 3 −18𝑥−12 4 𝑥 6 −12 𝑥 3 +9 q = r =

73 Polynomials The command polyfit is used for polynomial curve fitting.
p=polyfit(x,y,n) returns the coefficients for a polynomial p(x) of degree n that is a best fit (in a least-squares sense) for the data in y. The coefficients in p are in descending powers, and the length of p is n+1. [p,S]=polyfit(x,y,n) also returns a structure S that can be used as an input to polyval to obtain error estimates. x = linspace(0,4*pi,10); y = sin(x); p = polyfit(x,y,7); x1 = linspace(0,4*pi); y1 = polyval(p,x1); figure plot(x,y,'o') hold on plot(x1,y1) hold off For polynomial curve fitting with zero intercept you can find function file: polyfitZero(x,y,degree)

74 Polynomials The command roots is used generate polynomial roots.
r=roots(p) returns, in a column vector, the roots of the polynomial equations of the form: 𝑝 1 𝑥 𝑛 𝑝 𝑛 𝑥+ 𝑝 𝑛+1 =0 𝑥 2 +4𝑥=0 p=[1 4 0] r=roots(p)= 0 -4 𝑥 3 −3 𝑥 2 +5=0 p=[ ] r=roots(p)= i i i

75 Polynomials The command poly is used generate a polynomial with specified roots or characteristics p=poly(r) where r is a vector, returns the coefficients of the polinomial whose roots are the elements of r. 𝑥 2 +4𝑥=0 p=[1 4 0] r=roots(p)= 0 -4 r=[0 -4] p=poly(r)

76 Exercise 8: For the polynomial: 𝑝= 3𝑥 6 − 𝑥 4 + 2𝑥 3 +5𝑥+4
calculate the value of p when x equals to 3 5 and 7 calculate the coefficients of the derived polinomial p calculate the root of the polinomial p s = d = r = i i i i i i

77 Interpolation The command interp1 is used for interpolation of 1-D data. vq=interp1(x,v,xq) returns interpolated values of a 1D function defined by (x,v) points at the specific xq query points. It uses linear interpolation. vq=interp1(x,v,xq,method) specifies an alternative interpolation method: ‘nearest’, ‘next’, ‘previous’, 'linear','spline','pchip', 'makima', or 'cubic’. x=0:pi/4:2*pi; v=cos(x); xq=0:pi/12:2*pi; vq=interp1(x,v,xq); vq_1=interp1(x,v,xq,'spline'); subplot(1,2,1) plot(x,v, ':xb', xq,vq, '-or') xlim([0 2*pi]) subplot(1,2,2) plot(x,v, ':xb', xq,vq_1, '-or')

78 Exercise 9: For a velocity vector: v=[ ] the corresponding Resistance values are: R=[ ]*10^3. a) Find the coefficients of the fitting curve for the resistance and plot both curves like in figure a. b) Find the value of the resistance for velocity equal to 5,10.8 and 15.2 and plot the points like in figure b. fig a fig b

79 Input data file Different waves to import files in MATLAB workspace:
MATLAB desktop: Home  Variable  Import Data load(‘filename’) :Load variables from file into workspace filename can be .txt or .mat (which is default) If the filename has an extension other than .mat, the load function treats the file as ASCII data. ASCII file must contain a rectangular table of numbers, with an equal number of elements in each row. The file delimiter (the character between elements in each row) can be a blank, comma, semicolon, or tab character. The file can contain MATLAB comments(lines that begin with %). When loading a .txt file, it must contain only numerical characters, alphabetic ones will not be recognized and will give you errors. sequence of commands in script, gives you the possibility to input the name of the file every time you run the code InputFile = input('Enter data file name: ','s') ; InputFile = strcat(InputFile,'.txt') ; DATAFILE = importdata(InputFile) ; Data = DATAFILE.data ; L = Data(1,1) ; With these commands your input file may have alphabetic characters (as for row and columns titles) that will automatically be neglected and only numbers may be considered. NOTE: If the file txt file is copied from excel make sure you copy numbers with only values and not formulas

80 Function File Both scripts and functions allow you to reuse sequences of commands by storing them in program files. Scripts store commands exactly as you would type them at the command line. Functions provide more flexibility, primarily because you can pass input values and return output values. SCRIPT FILE: test x=[x1,x2] [y1 , y2] = function_name(x1,x2) FUNTION FILE: function_name function [output_1,output_2] = function_name(input_1,input_2) % operations using input arguments (input_1 and input_2) turn out output arguments (output_1 and output_2) output_1 = f( input_1); output_2 = f( input_2); end

81 Function File SCRIPT FILE: test FUNTION FILE: function_name
disp('Enter dimensions of side a and b of rectangle') a=input('first side a =') b=input('first side b =') [Area, perimeter, diagonal] = rettang(a,b) FUNTION FILE: function_name function [ A , p , d] = rettang (a , b) A = a * b; p = 2 * (a+b); d=sqrt (a^2 + b^2); end Enter dimensions of side a and b of rectangle first side a =2 a = 2 first side b =3 b = 3 Area = 6 perimeter = 10 diagonal = 3.6056

82 Exercise 10: For example: enter number of values of vector x = n = 5
sum_x = 25 mean_x = 5 max_x = 9 min_x = 1 Create a script that: defines n which is equal the length of a vector x makes you input n values of the vector x calls a function named operations create the function operations which calculates: sum of n elements of vector x mean value of vector x maximum value of vector x minimum value of vector x

83 D-SYSTEMATIC SERIES MODELS
D-SYSTEMATIC SERIES MODELS is one of the last systematic series of round bilge transom stern hull form, published by the Germany yard Howaldtswerke_Deutsche Werft (HDW), and consists of seven models D1 to D7. For defining model scale, the full-scale length was selected as Lpp = 90.0 m with a design speed of VS=25 kn, which corresponds to a Froude number of 𝐹𝑛= 𝑉 𝑠 gL =0.433 At a fixed model length of LPP = 6.00 m, the model scale of the series is λ = 15. Resistance in calm water, propulsion and load variation tests, performed for seven models, for Froude range of 0.15≤Fn≤0.8. The table shows ship main dimensions for the 7 hull forms. MODEL L B T L/B B/T CB CP D Dprop (m) (-) (t) D1 90 13.500 3.600 6.6666 3.7500 0.50 0.62 3.215 D2 14.581 3.888 6.1725 D3 13.943 3.486 6.4548 4.0000 D4 13.043 3.726 6.9004 3.5000 D5 13.225 3.527 6.8052 0.52 0.65 D6 14.285 3.809 6.3004 D7 13.659 3.415 6.5891

84 D-SYSTEMATIC SERIES MODELS

85 Resistance for D-Series
Index M = model Index S =ship Residual resistance coefficient is 𝐶𝑅𝑀 =𝐶𝑅𝑆=𝐶𝑇𝑀+𝐶𝐹𝑀 Frictional resistance coefficient, 𝐶𝐹𝑀 (𝑇𝑊) = 0.075/(log⁡𝑅𝑛𝑀(𝑇𝑊) – 2)2 𝐶𝐹𝑆 (𝑇𝑊) = 0.075/(log⁡𝑅𝑛𝑆(𝑇𝑊) – 2)2 Reynolds number 𝑅𝑛𝑀 𝑇𝑊 = 𝑉 𝑀 ∗ 𝐿𝑂𝑆 𝜈𝑀 𝑇𝑊 𝑅𝑛𝑆(𝑇𝑊) = 𝑉 𝑆 ∗ 𝐿𝑂𝑆 𝜈𝑆 𝑇𝑊 water temperature TW Bare hull resistance TW 15 °C 𝑅𝐻𝑀 = (𝐶𝑅𝑀+𝐶𝐹𝑀(15))∗𝑞𝑀 Dynamic force defined as follow 𝑞𝑀 = 𝜌𝑀∗ 𝑉 𝑀 2+ 𝑆 𝑀 /2

86 Resistance for D-Series
Additional resistance of the appendices 𝑅𝐴𝑃 = 𝑅𝑇𝑀 − 𝑅𝐻𝑀 following non-dimensional resistance function 𝑌𝑅𝑇 = 𝑅𝐻𝑀 + 𝑅𝐴𝑃 𝑅𝐻𝑀 = 𝑎𝑅𝑇0 + 𝑎𝑅𝑇1∗ 𝐹𝑛 𝐵𝑅𝑇 ∗ exp 𝐵𝑅𝑇 + 𝐶𝑅𝑇∗𝐹𝑛 aRT0 =1.115 ; aRT1 = ; BRT = 5 ; CRT = -20 Resistance of the appendices 𝑅𝐴𝑃 = (𝑌𝑅𝑇 – 1)∗𝑅𝐻𝑀(𝑇2) Roughness value is fixed to 𝐶𝐴 = scale 𝜆 = 15 Full-Scale Resistance for the appended hull for without propellers turning 𝑅𝑇𝑆 = ((𝐶𝑅 + 𝐶𝐹𝑆 + 𝐶𝐴)∗𝑞𝑀 + (1 –0.4)∗𝑅𝐴𝑃∗𝜌𝑆/𝜌𝑀)∗𝜆3 Speed of the ship from speed of the model 𝑉𝑆 = 𝑉𝑀∗ λ Effective power needed to tow the ship 𝑃𝐸=𝑅𝑇𝑆∗𝑉𝑆

87 Exercise 11: Importing the ship data files and resistance data files (from excel file D_series_data) for 3 hulls D1 D3 and D5 of the D-Series, in only one script : calculate the full scale Ship Resistance calculate the approximation coefficients for the calm water resistance with fitting curves of 3 order with zero intercept. plot in the same graph ship resistance obtained from point a) and fitted curve from the coefficients obtained from point b)

88 Exercise 11 data: Ship Data D1 D3 D5 Lmodel (m) Lship (m) scale
6 Lship (m) 90 scale 15 Dpropel model (m) 0.2143 tankDens (kg/m^3) 1000 seaDens (kg/m^3) Wetted surface (m^2) 5.4005 5.4657 5.3656 SeaVisc (m^2/s) 1.1873E-06 g (m/s^2) Resistance Data D1 D3 D5 Fn CTM x 10^3 CRM x 10^3 CFM x 10^3 0.15 3.8985 0.6194 3.2791 3.8994 0.6203 4.1566 0.8775 0.2 3.9984 0.8842 3.1143 4.0358 0.9215 4.1563 1.042 0.25 4.4092 1.4143 2.9948 4.4609 1.4661 4.5722 1.5774 0.3 4.753 1.8508 2.9023 4.7942 1.892 5.0248 2.1226 0.35 4.9028 2.0755 2.8273 4.919 2.0917 5.2991 2.4718 0.4 6.3438 3.5791 2.7647 6.012 3.2473 6.2113 3.4467 0.45 7.7948 5.0837 2.7112 7.6421 4.931 8.1106 5.3994 0.5 8.1886 5.524 2.6646 8.1952 5.5306 8.4736 5.809 0.55 8.1136 5.4901 2.6235 8.1598 5.5363 8.3015 5.678 0.6 7.831 5.2442 2.5868 7.8269 5.24 7.9914 5.4045 0.65 7.4116 4.8579 2.5537 7.3624 4.8087 7.5852 5.0315 0.7 6.859 4.3353 2.5237 6.8474 4.3237 7.0057 4.482 0.75 6.2504 3.7542 2.4962 6.3049 3.8088 6.3394 3.8432


Download ppt "Introduction to Matlab e Simulik"

Similar presentations


Ads by Google