代写AMATH 301, Winter 2025 Homework 3: Coding portion代做留学生Matlab编程

Homework 3: Coding portion

AMATH 301, Winter 2025

Due Friday, January 31, 2025, 11:59PM in Gradescope

20 points

1.  (6 points) In Naive Gaussian elimination, small roundoff error can become amplified and lead to non-

sensical results when a pivot value is small, even if it’s not identically zero.

We wish to solve the system:

where ϵ is a small, nonzero number.  Because the first pivot is ϵ, Naive Gaussian elimination will require division by a small number which may lead to large rounding error.

Copy the Naive Gaussian elimination and full Gaussian elimination (with partial pivoting) codes from Canvas,  contained within gausselim .ipynb.   Solve  the  system Ax  =  b  using the Naive  Gaussian elimination function, and we will call the solution xnaive  (the autograder will not check xnaive ). Then solve Ax = b again using the full Gaussian elimination function, which we call xfull  (again, not checked by autograder). If we consider xfull  to be the true value, then a measure of the error of xnaive  is:

error = ||xnaive — xfull || 2

where

is the 2-norm of x where

x = [ x1     x2     x3     x4     x5   ]T .

We will investigate what happens as ϵ shrinks.  Create a np.array object called epsvec containing the values 1, 0.1, 0.01, ··· , 10 15  (note: decreasing order). For each such value of ϵ, determine the error of xnaive  as explained above.  Store the corresponding error values in a np.array object called errorvec. Finally, include the following lines of code to the end of your code, which will create a log-log plot of log10 (ϵ) on the x-axis and log10 (error) on the y-axis. The general trend should make sense.

logeps  =  np.log10(epsvec)

logerror  =  np.log10(errorvec)

plt.plot(logeps,logerror,’-or’)

plt.xlabel(’$\log_{10}(\epsilon)$’)

plt.ylabel(’$\log_{10}( ||x_{naive}-x_{full}|| _2)$’)

2.  (7 points) Begin with full Gaussian elimination code from gausselim .ipynb in Canvas. Alter the code to keep track of the number of row flips performed in the process of the forward-elimination steps.  If we want to look for trends in the number of row flips, we could create a random A matrix of size n × n, and a random b vector of size n × 1, perhaps full of integers from 0 to 99, and solve Ax = b using Gaussian elimination.

Use the following code to create such random A and b matrices:

np .random .seed(1)  #ensures  your  random  numbers  are  the  same  as  the  autograder’s

A=np.random.randint(0,100,[n,n])

A=A.astype(np.float32)  #convert  integers  to  floating-point  objects  (decimals)

b=np.random.randint(0,100,[n,1])

if  n==256:

bseedcheck  =  b

Create anp.array object nvec which contains the values of n = 4, 8, 16, 32, 64, 128, 256 (7 values total). For each value of n, run your altered full Gaussian elimination code to record the percentage  of rows which were flipped, and store that value in the np.array object percentflips.  So, percentflips should contain 7 values, where the ith value is the percentage of rows flipped, when the length of b is the ith value of n.  By “percentage” we mean a number between 0 and 1 inclusive, not between 0 and 100. The general trend should make sense.

Make sure to reset the random number seed to 1 before creating each A, but do not reset the seed to 1 before creating each b (in other words, do not modify the order or content of the lines above– if you do, your random numbers will differ from the autograder’s.)  In order to check that the random numbers your code is producing are the same as the autograder’s, the autograder will check the vector bseedcheck, which is your vector for b when n = 256.  If that’s correct, it assumes that all the random A and b’s you created contain the right numbers.

3.  (7 points) In this problem we will solve Ax = b approximately using Jacobi iteration.  Use the following code to create a random b vector of size 10 × 1 as a np.array object called bjacobi, and a random strictly diagonally dominant A matrix of size 10 × 10 as anp.array object called Ajacobi. The solution x is a 10 × 1 vector.

np .random .seed(1)  #ensures  your  random  numbers  are  the  same  as  the  autograder’s

Ajacobi  =  0.01*np.random.randint(-50,51,[10,10])+10*np.eye(10)

bjacobi  =  np.random.randint(-50,51,[10,1])

Start at a guess of

x0  = [ 0   0   0 ··· 0 ]T

and use Jacobi iteration to repeatedly update xn ; call this new vector xn+1 .  The step distance from xn  to xn+1  can be quantified by the 2-norm:

step = ||xn+1 — xn || 2

Stop the Jacobi iteration as soon as the value of the step is below 10 5 .  Store the values of the steps in a np.array object called stepvec.  So, the first entry of stepvec should be ||x1 — x0 || 2 , the second entry is ||x2 — x1 || 2 , and so on; the last entry should be the only value smaller than 10 5 .

You can use as a starting point Kutz’s code below, which implemented Jacobi iteration on the system:

#  Jacobi  iterations

x  =  np.array([0]);  y  =  np.array([0]);  z  =  np.array([0])

for  j  in  range(100):

x  =  np.append(  x,  (7+y[j]-z[j])/4  )

y  =  np.append(  y,  (21+4*x[j]+z[j])/8  )

z  =  np.append(  z,  (15+2*x[j]-y[j])/5  )

if  abs(x[j+1]-x[j])  < 1e-5:

break

print(j)

print(x[j  +  1])

print(y[j  +  1])

print(z[j  +  1])





热门主题

课程名

mktg2509 csci 2600 38170 lng302 csse3010 phas3226 77938 arch1162 engn4536/engn6536 acx5903 comp151101 phl245 cse12 comp9312 stat3016/6016 phas0038 comp2140 6qqmb312 xjco3011 rest0005 ematm0051 5qqmn219 lubs5062m eee8155 cege0100 eap033 artd1109 mat246 etc3430 ecmm462 mis102 inft6800 ddes9903 comp6521 comp9517 comp3331/9331 comp4337 comp6008 comp9414 bu.231.790.81 man00150m csb352h math1041 eengm4100 isys1002 08 6057cem mktg3504 mthm036 mtrx1701 mth3241 eeee3086 cmp-7038b cmp-7000a ints4010 econ2151 infs5710 fins5516 fin3309 fins5510 gsoe9340 math2007 math2036 soee5010 mark3088 infs3605 elec9714 comp2271 ma214 comp2211 infs3604 600426 sit254 acct3091 bbt405 msin0116 com107/com113 mark5826 sit120 comp9021 eco2101 eeen40700 cs253 ece3114 ecmm447 chns3000 math377 itd102 comp9444 comp(2041|9044) econ0060 econ7230 mgt001371 ecs-323 cs6250 mgdi60012 mdia2012 comm221001 comm5000 ma1008 engl642 econ241 com333 math367 mis201 nbs-7041x meek16104 econ2003 comm1190 mbas902 comp-1027 dpst1091 comp7315 eppd1033 m06 ee3025 msci231 bb113/bbs1063 fc709 comp3425 comp9417 econ42915 cb9101 math1102e chme0017 fc307 mkt60104 5522usst litr1-uc6201.200 ee1102 cosc2803 math39512 omp9727 int2067/int5051 bsb151 mgt253 fc021 babs2202 mis2002s phya21 18-213 cege0012 mdia1002 math38032 mech5125 07 cisc102 mgx3110 cs240 11175 fin3020s eco3420 ictten622 comp9727 cpt111 de114102d mgm320h5s bafi1019 math21112 efim20036 mn-3503 fins5568 110.807 bcpm000028 info6030 bma0092 bcpm0054 math20212 ce335 cs365 cenv6141 ftec5580 math2010 ec3450 comm1170 ecmt1010 csci-ua.0480-003 econ12-200 ib3960 ectb60h3f cs247—assignment tk3163 ics3u ib3j80 comp20008 comp9334 eppd1063 acct2343 cct109 isys1055/3412 math350-real math2014 eec180 stat141b econ2101 msinm014/msing014/msing014b fit2004 comp643 bu1002 cm2030
联系我们
EMail: 99515681@qq.com
QQ: 99515681
留学生作业帮-留学生的知心伴侣!
工作时间:08:00-21:00
python代写
微信客服:codinghelp
站长地图