• Keine Ergebnisse gefunden

1 Hands on

N/A
N/A
Protected

Academic year: 2022

Aktie "1 Hands on"

Copied!
2
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

1 Hands on

Vectors

• Create a row vectorausing commanda = [ 1 2 3 4]

• Create a column vectorbusing commandb = [1;2;3;4]

• You can transpose a vector usingtranpose(b)or b’

• Scalar product can be done witha*b

• Term by term product can be done witha.*b

• Term by term dividing can be done witha./b

• Compute the norm-2 of a vector

• what doesa(2:end)-a(1:end-1) does ?

Matrices

• Type A = [1 2;3 4]to create the matrix

A= 1 2

3 4

.

• One can change to 17 the element on the second row, third column of that matrix withA(2,3)=17

• Use the functioneyeto create an identity matrix

• Look at the functionsdiag,ones, and create an identity matrix by using these two functions.

• By adding three matrices created with the diag function, create a tridiag- onal matrix

• Use the commandinvto invert a matrix

• To solve the linear problem Ax = b with matlab, one option is b=A\b (backslash is important : A\b6=b/A).

1

(2)

Solving your first numerical problem

We want to solve the stationary 1D heat equation on the domain Ω = [0,1]

with a source termf(x) =sin(2πx).

The problem we want to solve is : find T such that

2T

∂x2 =f on Ω T(0) =T(1) = 0

(1)

• Create a script that you will callheat1D.m.

• Create the space variables : N=11;dx=1/(N-1) ; x=[0:dx:1]’;This will create a column vector with the spatial coordinates.

• Create the source term withf = sin(2*pi*x);

• Create the following tridiagonal matrix

A=

1 0 · · · 0

1

∆x2

−2

∆x2 1

∆x2 0 · · · 0

0 . .. . .. . .. . .. . .. . .. 0 · · · 0 ∆x12

−2

∆x2 1

∆x2

0 · · · 0 1

with the command lines A = eye(N,N);

for i=2:N-1

A(i,i-1)= 1/dx^2;

A(i,i) = -2/dx^2;

A(i,i+1)= 1/dx^2;

end

• Solve the linear problemAT =f as indicated in the previous section.

• Using the commandsticandtoc, find the time needed to solve the linear problem for differentN, e.g. N=100, N=1000, N=10000.

• ChangeA = eye(N,N);into A = speye(N,N);. How much time does it take to solve the linear problem ? What are the advantages of a sparse matrix ?

• One can plot the solution withplot(x,T), ifTis the solution vector.

• Bonus : Find an other way to build the matrix, usingdiagorspdiag.

2

Referenzen

ÄHNLICHE DOKUMENTE

The “Draw Structure” option of the PubChem database allows you to depict your SMILES and SMARTS, which opens a separate

The Chemical Structure Search option of the PubChem database allows you to depict your SMILES and SMARTS using the Sketch button that opens a separate

The Chemical Structure Search option of the PubChem database allows you to depict your SMILES and SMARTS using the Sketch button that opens a separate

In light of the excavated remains of the experim ental sm elt, layers norm ally interpreted as infill may also contain heavily disturbed traces of sm elting activities. During

Cases where a page number was present in the template, but as static text, are considered to be relevant templates as they are likely to give different content from each other, while

Naturally fibre-rich foods such as vegetables, fruit, wholegrain products, nuts and seeds will help you to maintain your weight, while lots of fizzy drinks and sweets, white

In §4 we present our main result, namely, that a given Sonneveld pencil defines a one-dimensional family of purified pencils and a one-dimensional family of deflated pencils

While everybody concerned with Europe- an China policy is fixated on Washington’s next move, it has been barely noted that Japan, South Korea, and Taiwan all had some sort