0% found this document useful (0 votes)
14 views10 pages

Software Simulation Paper Chapter2u

Uploaded by

perlaperdido3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views10 pages

Software Simulation Paper Chapter2u

Uploaded by

perlaperdido3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Software Simulation

Software Simulation

Dr.Fatma Khammar

November 2023
5.0
Table of contents
Introduction 3

I - Data types and variables 4

1. Vectors in Matlab ............................................................................................................................................ 4

2. Matrices in Matlab .......................................................................................................................................... 5

3. Deleting row or column .................................................................................................................................. 7

4. Simplification of algebraic expression ............................................................................................................ 8


4.1. simplify , factor , collect , expand ................................................................................................................................................ 8

References 10
Introduction

Simulation software is used to simulate the dynamic behavior of a system that is represented by a mathematical
model. At each stage of the model simulation, the state of each part of the system is calculated using time-based or
event-based solvers. Typically, simulation software also includes visualization tools, such as data displays, that allow
the simulation to be monitored as it runs. Engineers and scientists use simulation software for many reasons:

- It is often less expensive and simpler to create and simulate a model than to create and test a hardware
prototype.

- If the hardware prototype is not available early enough in the development process, you can use simulation
software to explore the design space and test different scenarios as early as possible.

- Once the hardware prototype is available, you can connect it to simulation software to verify that the software
and hardware elements of the system communicate correctly.

Simulation is a virtual mathematical process involving a computer that processes input data representing very
specific “real-world” conditions. Ideally, you detail the constraints and loads to which the product will be subjected
and then, based on the results of the simulation, you correct the design defects or anticipate them. But simulation is
not just a tool for perfecting the design you have decided on. Simulation allows you to optimize your design, and is
different from the iteration process where you test each iteration. You can carry out feasibility and optimization
studies to achieve the objectives.

3
Data types and variables

Data types and variables


I
1. Vectors in Matlab
We have only seen scalar variables here (i.e. numbers) but Matlab allows you to use vectors or matrices as well.
Let's start with vectors. To define a vector v, for example

v = (11 22 33 44 55), we use

>> v = [11 22 33 44 55]

The command

>> v'

Then give the transpose of this vector, that is to say here a column vector. We can also write

>> z=[11 ;22 ;33 ;44 ;55]'

To directly obtain a column vector z, but it is much simpler to write

>> z = v'

Or

>> z = [11 22 33 44 55]'

>> vec = [1 :2 :10]'

defines the vector

To access a particular value of a vector, we will write for example

>> v(2)

Which simply gives access to the second element, i.e. here 22. It is also possible to use the “:” to select several
elements of a vector

>> v(1:3)

Gives access to elements going from 1 to 3

>> v (1:2:5)

4
Matrices in Matlab

Which gives access to elements that go from 1 to 5, but incrementing with a step of 2. In this case, the vector
(11 33 55) is returned.

>> v

The whole vector is returned, but we obtain the same result by

>> v (:)

The symbol “:” without further details meaning “the whole vector”. Matlab allows the multiplication of vectors,
however we will monitor the order of what we write: so if vec*vec, returns an error message, vec*vec' returns a
matrix and vec'*vec a number.

2. Matrices in Matlab
We have just seen our first matrix. In fact, it is enough to add a dimension to the vectors to create matrices with
the same notations. Thus

>> A=[1 2 3;2 4 5;6 7 8]

generate the matrix

It is important to pay attention to the size of the objects that you define otherwise you will get error messages.
If we want to create a matrix from its columns, simply write

>> A=[[1 2 6]'[2 4 7]'[3 7 8]']

To read the elements, we use the same method as before. So the order.

>> A(1,3)

Allows access to the element (1,3) of the matrix A, which here is 3. Likewise

>> A(1:3,2:3)

V=Linspace (n, m,p)

Another way to construct vectors in MATLAB is to use the linspace command (using parentheses) , the vector
V contains p numbers between n and m as follows:

>> b = linspace(0,5,10)

b=

Columns 1 through 10

0 0.5556 1.1111 1.6667 2.2222 2.7778 3.3333 4.4444 5.0000

length(y)

The length of a vector is the number of elements contained in the vector. In order to obtain the length of a
vector, we use the MATLAB command length as follows:

>> length(b)

ans =

10

5
Matrices in Matlab

sum(y)

In order to add the total sum of the values of the elements of a vector, we use the MATLAB command sum as
follows:

>> y = [ 1 3 0 5 -2 ]

y=

1 3 0 5 -2

>> sum(y)

ans =

min (x) : minimum

max (x) : maximum

In order to find the minimum value and the maximum value of the elements of a vector, we use the MATLAB
commands min and max, respectively, as follows:

>> y = [ 1 3 0 5 -2 ]

>> min(y)

ans =

-2

>> max(y)

Elements of a vector can be sorted using the MATLAB command sort as follows:

>> x = [2 1 -3 5 3]

x=

2 1 -3 5 3

>> sort(x)

ans =

-3 1 2 3 5

Sorting in the above example has been obtained in an ascending order

There are some MATLAB commands for statistics that can be used with vectors. For example, the MATLAB
commands mean, and median can be used to obtain the statistical values of range, mean, and median for a set of
numbers in a vector as follows:

Size (A) : number of rows and columns of A

[ y , I ] = sort (x) : also returns the clues of the elements of x

find (x) : return the non zero clues of x

[ y , I ] = find (x) : retuns rows ( in vector I ) and columns ( in vector J ) non zero elements

of x,

6
Deleting row or column

cumsum(x) : vector containing the cumulative sum of the elements of x

prod(x) : produces elements of x

cumprod(x) : vector containing the cumulative product of the elements of x

diff(x) : vector of the differences between two consecutive elements of x

mean(x) : average of the elements of x

median(x) : median

A(i1,i2)=[ ] : delete i1 to i2 coordinates of x

A(i1:i2,:) : lines i1 to i2 of A

A(i1:i2,:)=[ ] : delete lines i1 to i2 of A

A(:,j1:j2) : columns j1 to j2 of A

A(:,j1:j2)=[ ] : delete columns j1 to j2 of A

A row or a column of a matrix can be deleted by setting it to a null vector, [ ].

>> A(:,2)=[]

ans =

13

46

3. Deleting row or column


To delete a row or column of a matrix, use the empty vector operator, [ ].

>> A(3,:) = []

A=

123

456

Third row of matrix A is now deleted. To restore the third row, we use a technique for

creating a matrix

>> A = [A(1,:);A(2,:);[7 8 9]]

A=

123

456

789

Matrix A is now restored to its original form.

To determine the dimensions of a matrix or vector, use the command size. For example,

>> size(A)

ans =

7
Simplification of algebraic expression

33

means 3 rows and 3 columns.

Or more explicitly with,

>> [m,n]=size(A)

4. Simplification of algebraic expression

To simplify the expression

Such a calculation can be done with Matlab using:

sym : which constructs a symbolic expression from a character string,

simple : which simplifies this symbolic expression

4.1. simplify , factor , collect , expand


Also perform the transformation and simplification of these expressions, simple calling one by one all these
functions and finally rataining the simplest result ( the one corresponding to the shortest charater string)

- double

To get approximate numeric value of the expression, just use double:

>>double (s)

ans = 13.9282

- Example

P=n(n+1)(n+2)(n+3)+1

Where n designates an integer variable , we ask to calculate P for n=2 ,3 , 4 , 5

Then to show that P is always the square of an integer.

Where n designates an integer variable, we ask to calculate P for n=2 ,3 , 4 , 5

Then to show that P is always the square of an integer.

For this we build, using the sym command, asymbolic expression P in which the letter n appears.

8
simplify , factor , collect , expand

Then we use the subs command to replace n by 2 in the expression P. Finally the factor command allows you to
factor the result and put in evidence a perfect square.

We perform the same calculation to replace n by 3 , 4 and 5

To show that P is always the square of on integer, we directly factor the expression P:

The result always shows that P is always the square of the integer

- Using syms

A simple variable ( mathematics) such that, n , x , θ , is itself an elementary symbolic expression and it is often
convenient to assign it to a variable (computer) of the same name:

>> n = syms (‘n') ; x = syms (‘x') ; theta = syms (‘ theta')

We can replace this sequence of affection by the only command

>> syms n x theta

We can then perform calculations using these variables

9
References

References
Introduction to MATLAB for Engineers
1 William J. Palm III “University of Rhode Island “

MATLAB
2 The Language of Technical Computing

10

You might also like