讲解:EEEN10036、C++、Programming、c/c++C/C++|SPSS

EEEN100362019-20C Programming Mini AssignmentPlease read carefully - this assignment is assessed and is worth up to 10% ofthe overall course unit mark. You may choose between two versions which areworth different percentages of the available marks: a simplified version (A1,worth 40%, see Appendix 1) and a full version (A2, worth 80%, see Appendix2). A skeleton program of each version is provided on Blackboard to help youget started. In addition there are some unseen questions based on theassignment which must be answered at the time of the assessment. Thesequestions are worth 20%, thus the maximum mark available for A1 is 60%, andthe maximum available for A2 is 100%. The mark schemes are at the end ofthis document.IntroductionKirchhoff’s current and voltage laws produce a set of linear equations for the currentsand voltages in resistor circuits. When the circuits are small, they can be solved byhand, however for larger circuits it is more convenient to use a program. The set oflinear equations which result from Kirchhoff’s current and voltage laws can berepresented mathematically in matrix – vector1form and there are a number ofdifferent methods for solving them. In this assignment, you’ll investigate how torepresent a circuit as a set of linear equations in matrix–vector form and solve themusing a technique known as Jacobi iteration.Problem Description2A track circuit (Figure 1) feeds current to a relay, and consists of ten sections. It hasa 10V supply, in which the wires to/from the supply have a resistance of 5 ohms. Theresistance of the feed wires to each section is 0.03 ohms, and the resistance acrosseach section is 40 ohms. The relay and its feed wires have a total resistance of 10ohms. The problem is to determine the currents i0,i1,…i10 in the circuit.Figure 1. Track circuit.A simple guide to the matrix mathematics needed for this assignment (A2 version) is provided in Appendix 3.This problem has been adapted from detecting a train on a track:Set of Linear Equations DescriptionKirchhoff’s laws can be used in several ways to solve this problem, but one thatgives a simple set of linear equations is now described. Let 𝑖0, 𝑖1, … , 𝑖9, be the currentin the successive sections and 𝑖10 the current to the relay. The current between thefirst and second sections is 𝑖0 − 𝑖1 so that the voltage drop across the sections is40(𝑖0 − 𝑖1) volts. The first equation below uses this and the voltage drop in the feedwires. The remaining nine equations compare the voltage drop across successivesections with the drop in the two feed wires, and the last equation compares thevoltage drop across the last section with that in the relay.40(𝑖0 − 𝑖1) + 5.06𝑖0 = 1040(𝑖0 − 𝑖1) = 0.06𝑖1 + 40(𝑖1 − 𝑖2)40(𝑖1 − 𝑖2) = 0.06𝑖2 + 40(𝑖2 − 𝑖3)⋮40(𝑖8 − 𝑖9) = 0.06𝑖9 + 40(𝑖9 − 𝑖10)40(𝑖9 − 𝑖10) = 10𝑖10It is convenient to rearrange and write these 11 equations as follows:45.06𝑖0 − 40𝑖1 = 1040𝑖0 − 80.06𝑖1 + 40𝑖2 = 040𝑖1 − 80.06𝑖2 + 40𝑖3 = 0⋮40𝑖8 − 80.06𝑖9 + 40𝑖10 = 040𝑖9 − 50𝑖10 = 0The coefficients can then be represented mathematically by an 11-row x 11-columnmatrix A and an 11x1 column vector v:In the above, 𝑨 is the (11x11) matrix of resistance values and 𝒗 is the (11x1) columnvector where the first entry is 10 (the supply voltage) and the other entries are zero.Each row of 𝑨 represents a section and will have, at most, 3 non-zero values. Theserepresent the interactions with the immediate neighbouring sections.Vector v is produced by multiplying 𝑨 with a second column vector 𝒙 as follows:where 𝒙 contains the unknown currents with entries 𝑖0, 𝑖1, … 𝑖10. The elements of vare produced by multiplying each element of A with x as described in Appendix 3(matrix-vector multiplication). Computationally, A can be represented by a 2-D array,and v and x by 1-D arrays, where row and column numbers always start at 0.Jacobi Method DescriptionThe Jacobi method is a relatively simple iterative technique for solving a set of linearequations represented in the matrix–vector form: 𝑨𝒙 = 𝒗.With the Jacobi method we start with an initial estimate of the current vector 𝒙, andthen iteratively try and improve upon it. For the 𝑘𝑡ℎiteration of the Jacobi method,the estimates (vector 𝒙𝑘) are updated as𝑥𝑖𝑘 =(𝑣𝑖 − ∑𝑎𝑖𝑗𝑥𝑗𝑘−1𝑛𝑗=1,𝑗≠𝑖 )/𝑎𝑖𝑖where 𝑎𝑖𝑗 is the 𝑖,𝑗𝑡ℎ element of the matrix 𝑨, 𝑣𝑖is the 𝑖𝑡ℎ element of thevector 𝒗 and 𝑥𝑖𝑘is the current (𝑘𝑡ℎ) estimate of the value of 𝑥𝑖. Generally, Jacobismethod begins with a zero vector (𝑥0 = 0) and iterates until the estimated value ofthe unknown parameter vector, 𝑥𝑘, doesnt change significantly on successiveiterations. One way to measure how much the vector changes is with the followingexpression:𝑑𝑘 = √∑ (𝑥𝑖𝑘 − 𝑥𝑖𝑘−1)2𝑛𝑖=0which measures the distance between successive Jacobi estimates. With theJacobi iteration convergence can be slow, requiring several hundred steps. For thisassignment, convergence is achieved when the differences between steps is lessthan or equal to a value defined as the tolerance.Several other explanations of the Jacobi method can be found online, for example:https://en.wikipedia.org/wiki/Jacobi_methodAssignment DetailsIn this assignment you will write a C program which calculates the currents in thecircuit using the Jacobi method. There are two versions: A1 and A2 which aredetailed in Appendix 1 and 2, respectively.For both A1 and A2, a skeleton of the final program is provided to help you getstarted. In each case you must follow the instructions in the comments to: Construct the arrays and vectors which represent the set of linear equations. Use the Jacobi method to determine the unknown current vector. continued..4 Monitor the convergence of the iterative scheme. The test for convergencemust use the expression for 𝑑𝑘 given above in the previous section JacobiMethod Description. Print out the matrix A and the final values of the current vector, including thenumber of iterations needed to converge upon the solution for a giventolerance. Examples of the expected output are provided in the Appendices.In version A2 the circuit data is read from a file called input.txt which has thefollowing format:𝑣0, 𝑣1, … , … , … , … , … , … , … , 𝑣(𝑁−2), 𝑣(𝑁−1)𝑎00, 𝑎01, … , … , … , … , … , … , 𝑎1(𝑁−2), 𝑎0(𝑁−1)𝑎10, 𝑎11, … , … , … , … , … , … , 𝑎1(𝑁−2), 𝑎1(𝑁−1)… , … , … , … , … , … , … , … , … , … , … , … , … , …… , … , … , … , … , … , … , … , … , … , … , … , … , …𝑎(𝑁−2)0, 𝑎(𝑁−2)1, … , 𝑎(𝑁−2)(𝑁−2), 𝑎(𝑁−2)(𝑁−1)𝑎(𝑁−1)0, 𝑎(𝑁−1)1, … , 𝑎(𝑁−1)(𝑁−2), 𝑎(𝑁−1)(𝑁−1)where each line is a comma-separated list of N values (with no spaces anywhere onthe line) such that the first line contains the voltage vector v data, and the remaininglines are the matrix A coefficients. A function read_data_from_file() is provideEEEN10036作业代做、代写C++编程设计作业、Programming作业代做、代写c/c++语言作业 帮做C/C+d in theskeleton for reading the data. In the assignment N=11, however you will be asked toshow that your program works with different sized data.Deadlines and Marking Schemesa) The assessment will take place at the start of your 2-hour lab session in week10 (i.e. w/c Monday 20th April 2020), and at no other time.b) The assignment is individual and will be conducted under exam conditions.Plagiarism or collusion will result in zero marks and the matter will be reportedfor academic malpractice.c) The assessment will consist of demonstrating your solution to an examiner.Afterwards you will be asked to upload your solution to Blackboard and thenanswer one or more previously unseen questions based on the assignment.d) Important: you are expected to have completed the assignment before youarrive. You must be able demonstrate your program when asked. You are notpermitted to use the lab time to work on the assignment.e) This is a closed-book assessment, taken under examination conditions.f) You must implement the program as described. If not, marks may bededucted. See the marking scheme below.g) The examination will take place during your usual lab session, which will havetasks that you should aim to complete (there might also be an assessed quizto complete on the day).h) There may be some waiting on the day due to the limited number ofexaminers and your patience is appreciated.5Marking SchemesA1A1 Version MarkDoes the program correctly calculate and print out the dataas expected? 2Fully able to explain how the program works? 1The program correctly implements and uses themagnitudeVectorDiff function.1Answers previously unseen question(s). 2Total 6A2Item MarkDoes the program correctly calculate and print out the dataas expected? 3Fully able to explain how the program works? 1The program correctly implements and uses themagnitudeVectorDiff function.1The program correctly implements and uses theJacobiMethod function.2Does the program work with an alternative data set? 1Answers previously unseen questions. 2Total 10Marks will be deducted for both A1 and A2 versions as follows:Item MarkOne or more compiler warnings. 1The appearance of the program is poor, forexample is poorly structured, or is notindented, or has very few comments.1The program uses one or more globalvariables other than those provided in theskeleton.16Appendix 1: Version A1Starting with the given set of linear equations45.06𝑖0 − 40𝑖1 = 1040𝑖0 − 80.06𝑖1 + 40𝑖2 = 040𝑖1 − 80.06𝑖2 + 40𝑖3 = 0⋮40𝑖8 − 80.06𝑖9 + 40𝑖10 = 040𝑖9 − 50𝑖10 = 0re-write them as:Then make an initial guess of the solution: x(0) = (x0(0),x1(0),x2(0) ,…xn(0)). Substitutethese values into the right hand side the of the rewritten equations to obtain the firstapproximation: (1). This accomplishes one iteration.Similarly, the second approximation (x(2)= (x0(2),x1(2),x2(2),…xn(2)) is computed bysubstituting the first approximation’s values into the right hand side. By repeatediterations, we form a sequence of approximations x,k=0,1,2,..n. Two 1-D arrays can be used to store the data values of x(k+1)and x(k).ExampleConsider a simpler example with a set of 3 equations:4x0 + 2x1 + 3x2 = 83x0 - 5x1 + 2x2 = -14 - 2x0 + 3x1 + 8x2 = 27Expressing this in a form suitable for iteration, we have:x0 = (8 – 2x1 – 3x2) / 4x1 = (-14 – 3x0 – 2x2) / (-5)x2 = (27 + 2x0 – 3x1) / 8Beginning with x0 = x1 = x2 = 0 the first iteration givesx0 = (8 – 2x0 - 3x0) / 4 = 2.0x1 = (-14 – 3x0 - 2x0) / (-5) = 2.8x2 = (27 + 2x0 - 3x0) / 8 = 3.375The second iteration givesx0 = (8 – 2x2.8 - 3x3.375) / 4 = -1.931x1 = (-14 – 3x2.0 - 2x3.375) / (-5) = 5.350x2 = (27 + 2x2.0 - 3x2.8) / 8 = 2.825etc. After around 45 iterations the values settle down to x0=-1.000, x1=3.000 andx2=2.000 (to 3 decimal places), which can be verified by substitution.7Finally, for the data used in the assignment, the expected output is shown below:Appendix 2: Version A2In this version everything is expressed in matrix-vector form. Recall that a set oflinear equations can be represented in matrix–vector form as 𝐀𝐱 = 𝐯. Recall that Ais an NxN matrix and x and v are Nx1 column vectors. The approach in this methodrequires that A be split into a diagonal matrix D and two triangular matrices L and U.As an example, consider the following 3x3 matrix A, where the elements arowcol aresubscripted according to their row-column position. A is split into a diagonal matrixD, and two further matrices L (lower) and U (upper):a00 a01 A02 a00 0 0 0 0 0 0 a01 a02A = a10 a11 a12 = 0 a11 0 + a10 0 0 + 0 0 a12a20 a21 a22 0 0 a22 a20 a21 0 0 0 0i.e. A = D + L + UNext rewrite Ax = v as (D + L + U)x = v and re-arrange to giveDxn+1 = v − (L + U)xn-thus new values of xn+1 are generated from values xn obtained in the previous step.By taking the inverse D-1of D we can writexn+1 = −D−1(L + U)xn + D−1vwhere D-1is simply the diagonal matrix of the inverse of the diagonal elements of A.Using as an example a 3x3 matrix, this last expression is represented as follows: x0(n+1) = 1/a00 0 0 0 a01 a02 x0(n) 1/a00 0 0 v0 x1(n+1) = – 0 1/a11 0 . a10 0 a12 . x1(n) + 0 1/a11 0 . v1 x2(n+1) = 0 0 1/a22 a20 a21 0 x2(n) 0 0 1/a22 v2v. This is now in a convenient form for Jacobiiteration.8Example: Starting with 4 2 3 x0 8 3 -5 2 . x1 = -14 -2 3 8 x2 27i.e. A . x = vSplitting A into D, L and U and taking the inverse of D: x0(n+1) = 1/4 0 0 0 2 3 x0(n) 1/4 0 0 8 x1(n+1) = – 0 -1/5 0 . 3 0 2 . x1(n) + 0 -1/5 0 . -14 x2(n+1) = 0 0 1/8 -2 3 0 x2(n) 0 0 1/8 27 xn+1 = -D−1(L + U) x + D−1 vAfter obtaining T and c and ~80 iterations, we find that x = -1.000 3.000Finally, for the data used in the assignment, 2.000the expected output is shown below:9Appendix 3 A simple guide to matrix maths.The mathematical operations involving matrices are straightforward, and examplesof matrix-vector and matrix-matrix multiplication are shown below. If you prefer avideo explanation, there are plenty of examples online, for example Khan Academy.In a matrix, data is organised into n rows by m columns (in this assignment n=m). Itis natural to represent a matrix in a program by a 2-D array, and process the datawith a pair of nested for-loops.(i) Matrix-vector multiplication: this is accomplished by multiplying eachelement of each row of a matrix A with the corresponding element in acolumn vector x; this produces a new vector:(ii) Matrix-matrix addition: elements in corresponding positions are added toproduce a new matrix. Consider the addition of two matrices A and B:(iii) Matrix-matrix multiplication: this operation is similar to (i), if one views Bas a collection of column vectors. Consider multiplying A and B-as shown below, this produces a new matrix:转自:http://www.daixie0.com/contents/13/4933.html

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,470评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,393评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,577评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,176评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,189评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,155评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,041评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,903评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,319评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,539评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,703评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,417评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,013评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,664评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,818评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,711评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,601评论 2 353

推荐阅读更多精彩内容