Главная » Просмотр файлов » MacKinnon - Computational Physics

MacKinnon - Computational Physics (523159), страница 7

Файл №523159 MacKinnon - Computational Physics (MacKinnon - Computational Physics) 7 страницаMacKinnon - Computational Physics (523159) страница 72013-09-15СтудИзба
Просмтор этого файла доступен только зарегистрированным пользователям. Но у нас супер быстрая регистрация: достаточно только электронной почты!

Текст из файла (страница 7)

Unfortunately this varies from language to language: in C(++) and Pascal the first index variesslowest and the matrix is stored one complete row after another; whereas in FORTRAN the first index variesfastest and the matrix is stored one complete column after another. It is generally most efficient to accessthe matrix elements in the order in which they are stored. Hence the simple matrix algebra, A B C,should be written in C1 as 1 In C(++) it is usual to use double instead of float when doing numerical programming. In most cases single precision is notaccurate enough.Matrix Algebra27const int N =??;int i, j;double A[N][N], B[N][N], C[N][N];for( i = 0; i < N; i++)for( j = 0; j < N; j++)A[i][j] = B[i][j] + C[i][j];or its equivalent using pointers.

In FORTRAN90 this should be writteninteger :: i,jinteger, parameter :: N = ??real, double precision :: A(N,N), B(N,N), C(N,N)do i = 1,Ndo j=1,NA(j,i) = B(j,i) + C(j,i)end doend doNote the different ordering of the loops in these 2 examples. This is intended to optimise the order in whichthe matrix elements are accessed.

It is perhaps worth noting at this stage that in both C++ and FORTRAN90it is possible to define matrix type variables (classes) so that the programs could be reduced tomatrix A(N,N), B(N,N), C(N,N);A = B + C;The time taken to add or subtract 2# matrices is generally proportional to the total number ofmatrix elements, , although this may be modified by parallel architecture.¥3.3.2 Multiplication of MatricesUnlike addition and subtraction of matrices it is difficult to give a general machine independent rule forthe optimum algorithm for the operation. In fact matrix multiplication is a classic example of an operationwhich is very dependent on the details of the architecture of the machine.

We quote here a general purposeexample but it should be noted that this does not necessarily represent the optimum ordering of the loops.const int L = ??, M = ??, N = ??;int i, j, k;double A[L][N], B[L][M], C[M][N], sum;for ( j = 0; j < N; i++)for ( i = 0; i < L; j++){for ( sum = 0, k = 0; k < M; k++)sum += B[i][k] * C[k][j];A[i][j] = sum;}in C.The time taken to multiply 2 modified by parallel processing.¥$matrices is generally proportional to ‡ , although this may be3.4 Elliptic Equations — Poisson’s EquationAt this point we digress to discuss Elliptic Equations such as Poisson’s equation.

In general these equationsare subject to boundary conditions at the outer boundary of the range; there are no initial conditions, suchas we would expect for the Wave or Diffusion equations. Hence they cannot be solved by adapting themethods for simple differential equations.Matrix Algebra283.4.1 One Dimension0Y½ jk"]KÑ&\[K 0We start by considering the one dimensional Poisson’s equation(3.2)½ QeT $ f ½ Q ½ QS#T O K 0 šYj Q(3.3)j Q jk"]KÅhK Q 8gO KÑ& .where we defineõ½ "<KÑ&U½ at KÅK and ½I"]KÑ&;½ S#T at KõK S#T ,The boundary conditions are usually of the form½ and ½ S#T are both known the 8 althoughsometimes the condition is on the first derivative. Since equations (3.3) may be written asand 8$ f ½£T ½ K 0 š‰jTU$}½(3.4)½ eTU$ f ½ 0 OO K 0 š‰j $}½ S#T†[(3.5)The 2nd derivative may be discretised in the usual way to giveThis may seem trivial but it maintains the convention that all the terms on the left contain unknowns andeverything on the right is known.

It also allows us to rewrite the (3.3) in matrix form as%&(*) %&(*)%&(*)&) &)&)&) &)&)&) &)&)&) &)&)&) &)&)&) &)&)&)&)&)......... ..&) & ..)& ..)&) &)&)(3.6)&) &)&)&) &)&)........ .......'+ ' +'+$f $ f $ f½£T½0½‡½ÑQ $f $ f $ fwhich is a simple matrix equation of the formAx½ eT½O K1K10ñ0ñš‰š‰jj 0TU$G½1O K10ñš‰j ‡OO K 0 š‰j‰QO K1K10ñ0ñš‰š‰jj e$}T ½ S#TOb(3.7)in which A is tridiagonal. Such equations can be solved by methods which we shall consider in section 3.5.For the moment it should suffice to note that the tridiagonal form can be solved particularly efficiently andthat functions for this purpose can be found in most libraries of numerical functions.There are several points which are worthy of note.We could only write the equation in this matrix form because the boundary conditions allowed us toeliminate a term from the 1st and last lines.Periodic boundary conditions, such ascan be implemented, but they have theeffect of adding a non–zero element to the top right and bottom left of the matrix, and , sothat the tridiagonal form is lost.½"<K Ÿ&o ½I"]KÑ&TTIt is sometimes more efficient to solve Poisson’s or Laplace’s equation using Fast Fourier Transformation (FFT).

Again there are efficient library routines available (Numerical Algorithms Group n.d.).This is especially true in machines with vector processors.3.4.2 2 or more DimensionsPoisson’s and Laplace’s equations can be solved in 2 or more dimensions by simple generalisations of theschemes discussed for 1D. However the resulting matrix will not in general be tridiagonal. The discretisedform of the equation takes the form½ ù á QegT ½ ù á QS#T ½ ù ge T\á Q ½ ù S#T^á Q,$ × ½ ù á Q, O K 0 †š j ù á QÑ[(3.8)Matrix Algebra29/ R: 8" : & : " : f & : [[ [" /: & : " f : & : " f : f & : [[ [ " f :/ & : [[ [" : & : " : f & : [ [ [†" : &The 2 dimensional index pairs ,.- may be mapped on to one dimension for the purpose of setting upthe matrix.

A common choice is so–called dictionary order,Alternatively Fourier transformation can be used either for all dimensions or to reduce the problem totridiagonal form.3.5 Systems of Equations and Matrix InversionWe now move on to look for solutions of problems of the form¥where A is ancase where B is an¥matrixand X and B areunit matrix.AX¥B(3.9)matrices. Matrix inversion is simply the special3.5.1 Exact MethodsMost library routines, for example those in the NAG (Numerical Algorithms Group n.d.) or LaPack (LapackNumerical Library n.d.) libraries are based on some variation of Gaussian elimination. The standardprocedure is to call a first function which performs an LU decomposition of the matrix A,ALU(3.10)where L and U are lower and upper triangular matrices respectively, followed by a function which performsthe operationsYX L e e T T BU(3.11)Y(3.12)on each column of B.The procedure is sometimes described as Gaussian Elimination.

A common variation on this procedure is partial pivoting. This is a trick to avoid numerical instability in the Gaussian Elimination (or LUdecomposition) by sorting the rows of A to avoid dividing by small numbers.There are several aspects of this procedure which should be noted:The LU decomposition is usually done in place, so that the matrix A is overwritten by the matricesL and U.The matrix B is often overwritten by the solution X.A well written LU decomposition routine should be able to spot when A is singular and return a flagto tell you so.Often the LU decomposition routine will also return the determinant of A.Conventionally the lower triangular matrix L is defined such that all its diagonal elements are unity.This is what makes it possible to replace A with both L and U.Some libraries provide separate routines for the Gaussian Elimination and the Back–Substitutionsteps.

Often the Back–Substitution must be run separately for each column of B.Routines are provided for a wide range of different types of matrices. The symmetry of the matrix isnot often used however.0‡The time taken to solve equations in unknowns is generally proportional to for the Gaussian–Elimination (LU–decomposition) step. The Back–Substitution step goes as for each column of B. Asbefore this may be modified by parallelism.Matrix Algebra303.5.2 Iterative MethodsAs an alternative to the above library routines, especially when large sparse matrices are involved, it ispossible to solve the equations iteratively.The Jacobi Method€(3.13)ð A Ò0D L Uwhere D is a diagonal matrix(i.e.) and L and U are strict lower and upper triangular21 Ä forforall Ò ). â Wematrices respectively (i.e.now write the Jacobi or Gauss–Jacobi iterativeprocedure to solve our system of equations asQS#T eT $Š" & QXD uBL U X v(3.14)QQ this procedure requires thewhere the superscripts refer to the iteration number.

Note that in practicestorage of the diagonal matrix, D, and a function to multiply the vector, X by L U.We first divide the matrix A into 3 parts+This algorithm resembles the iterative solution of hyperbolic (see section 2.3) or parabolic (see section 2.5) partial differential equations, and can be analysed in the same spirit. In particular care must betaken that the method is stable.Simple C code to implement this for a 1D Poisson’s equation is given below.int i;const int N = ??; // incomplete codedouble xa[N], xb[N], b[N];while ( ...

) // incomplete code{for ( i = 0; i < N; i++ )xa[i] = (b[i] - xb[i-1] - xb[i+1]) * 0.5;for ( i = 0; i < N; i++ )xb[i] = xa[i];}Note that 2 arrays are required for X and that the matrices, D, L and U don’t appear explicitely.The Gauss–Seidel MethodQQS#TQS=TAny implementation of the Jacobi Method (3.14) above will involve a loop over the matrix elements in eachcolumn of X. Instead of calculating a completely new matrix Xat each iteration and then replacingX with it before the next iteration, as in the above code, it might seem sensible to replace the elements ofX with those of Xas soon as they have been calculated.

Характеристики

Тип файла
PDF-файл
Размер
324,07 Kb
Тип материала
Учебное заведение
Неизвестно

Список файлов книги

Свежие статьи
Популярно сейчас
Как Вы думаете, сколько людей до Вас делали точно такое же задание? 99% студентов выполняют точно такие же задания, как и их предшественники год назад. Найдите нужный учебный материал на СтудИзбе!
Ответы на популярные вопросы
Да! Наши авторы собирают и выкладывают те работы, которые сдаются в Вашем учебном заведении ежегодно и уже проверены преподавателями.
Да! У нас любой человек может выложить любую учебную работу и зарабатывать на её продажах! Но каждый учебный материал публикуется только после тщательной проверки администрацией.
Вернём деньги! А если быть более точными, то автору даётся немного времени на исправление, а если не исправит или выйдет время, то вернём деньги в полном объёме!
Да! На равне с готовыми студенческими работами у нас продаются услуги. Цены на услуги видны сразу, то есть Вам нужно только указать параметры и сразу можно оплачивать.
Отзывы студентов
Ставлю 10/10
Все нравится, очень удобный сайт, помогает в учебе. Кроме этого, можно заработать самому, выставляя готовые учебные материалы на продажу здесь. Рейтинги и отзывы на преподавателей очень помогают сориентироваться в начале нового семестра. Спасибо за такую функцию. Ставлю максимальную оценку.
Лучшая платформа для успешной сдачи сессии
Познакомился со СтудИзбой благодаря своему другу, очень нравится интерфейс, количество доступных файлов, цена, в общем, все прекрасно. Даже сам продаю какие-то свои работы.
Студизба ван лав ❤
Очень офигенный сайт для студентов. Много полезных учебных материалов. Пользуюсь студизбой с октября 2021 года. Серьёзных нареканий нет. Хотелось бы, что бы ввели подписочную модель и сделали материалы дешевле 300 рублей в рамках подписки бесплатными.
Отличный сайт
Лично меня всё устраивает - и покупка, и продажа; и цены, и возможность предпросмотра куска файла, и обилие бесплатных файлов (в подборках по авторам, читай, ВУЗам и факультетам). Есть определённые баги, но всё решаемо, да и администраторы реагируют в течение суток.
Маленький отзыв о большом помощнике!
Студизба спасает в те моменты, когда сроки горят, а работ накопилось достаточно. Довольно удобный сайт с простой навигацией и огромным количеством материалов.
Студ. Изба как крупнейший сборник работ для студентов
Тут дофига бывает всего полезного. Печально, что бывают предметы по которым даже одного бесплатного решения нет, но это скорее вопрос к студентам. В остальном всё здорово.
Спасательный островок
Если уже не успеваешь разобраться или застрял на каком-то задание поможет тебе быстро и недорого решить твою проблему.
Всё и так отлично
Всё очень удобно. Особенно круто, что есть система бонусов и можно выводить остатки денег. Очень много качественных бесплатных файлов.
Отзыв о системе "Студизба"
Отличная платформа для распространения работ, востребованных студентами. Хорошо налаженная и качественная работа сайта, огромная база заданий и аудитория.
Отличный помощник
Отличный сайт с кучей полезных файлов, позволяющий найти много методичек / учебников / отзывов о вузах и преподователях.
Отлично помогает студентам в любой момент для решения трудных и незамедлительных задач
Хотелось бы больше конкретной информации о преподавателях. А так в принципе хороший сайт, всегда им пользуюсь и ни разу не было желания прекратить. Хороший сайт для помощи студентам, удобный и приятный интерфейс. Из недостатков можно выделить только отсутствия небольшого количества файлов.
Спасибо за шикарный сайт
Великолепный сайт на котором студент за не большие деньги может найти помощь с дз, проектами курсовыми, лабораторными, а также узнать отзывы на преподавателей и бесплатно скачать пособия.
Популярные преподаватели
Добавляйте материалы
и зарабатывайте!
Продажи идут автоматически
6309
Авторов
на СтудИзбе
313
Средний доход
с одного платного файла
Обучение Подробнее