Question

HW9-9 Write a python script to solve the Laplace equation in two dimensions.

code that I started:

# THIS IS NEEDED TO USE ARRAYS IN PYTHON:
from array import *
# THIS IS NEEDED TO MAKE PLOTS IN PYTHON:
import matplotlib.pyplot as plt
# IMPORT A ROUTINE FROM NUMPY USEFUL FOR CREATING AN
# ARRAY FILLED WITH ZEROS
from numpy import zeros

# THIS COMMAND CREATES AN ARRAY OF ZEROS WITH DESIRED SHAPE:
V=zeros([31,21])
newV=zeros([31,21])

# SET UP THE BOUNDARY VALUES: V=8 ON TOP OF BOX
iy=20
for ix in range(0,31):
V[ix,iy]=8.0

# DO THE ITERATIONS (MAY WANT TO ADJUST Nt)
Nt=100
for it in range(0,Nt):
for iy in range(1,20):
for ix in range(1,30):

.... I dont know how to finish ....

**** please help me complete the script please

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I'm providing two solutions :-

#1

from visual import*
from visual.graph import*
import numpy as np

 x = y = 7
del = 2

vtop = [-1,-.67,-.33,.00,.33,.67,1]
vbottom = [-1,-.67,-.33,.00,.33,.67,1]
vleft = -1
vright = 1

vguess= 0

x,y = np.meshgrid(np.arange(0,lenx), np.arange(0,leny))

v = np.empty((x,y))
v.fill(vguess)

v[(y-1):,:] = vtop
v [:1,:] = vbottom
v[:,(x-1):] = vright
v[:,:1] = vleft

boundary = 500

for iteration in range (0,boundary):
    for i in range(1,x):
        for j in range(1,y-1):
            v[i,j] = 
                      v[i,j] = .25*(v[i+1][j] + v[i-1][j] + v[i][j+1] + v[i][j-1])
            print (v)

#2

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import time
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm


plt.ion()
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_zlim(-1.01, 1.01)


def draw_plot(x, y, u):
    ax.clear()
    ax.set_zlim(-1.01, 1.01)
    ax.plot_surface(x, y, u, rstride=1, cstride=1, cmap=cm.coolwarm,
                    linewidth=0, antialiased=True)
    plt.pause(1e-5)


# Create  grid
m = 21
mesh_range = np.arange(-1, 1, 2/(m-1))
x, y = np.meshgrid(mesh_range, mesh_range)

# base case
u = np.exp(-5 * (x**2 + y**2))

draw_plot(x, y, u)

n = list(range(1, m-1)) + [m-2]
e = n
s = [0] + list(range(0, m-2))
w = s


def pde_step(u):
    """ PDE calculation at a single time step t  """
    return (u[n, :]+u[:, e]+u[s, :]+u[:, w])/4.


k = 5
U_step = u

for it in range(500):
    U_step = pde_step(U_step)

    # Every k steps, draw the graphics
    if it % k == 0:
        draw_plot(x, y, U_step)

while True:
    plt.pause(1e-5)
Add a comment
Know the answer?
Add Answer to:
code that I started: # THIS IS NEEDED TO USE ARRAYS IN PYTHON: from array import * # THIS IS NEEDED TO MAKE PLOTS IN PY...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need a Python code for this problem. We can use python's array slicing in many...

    I need a Python code for this problem. We can use python's array slicing in many ways, and here is just one example. To take the forward derivative of an array y, we use (y[i+1] - y[i])/dx For example, if dx=1 , we might write a derivative routine as yderiv = zeros (len(y)-1) for i in range(len(y)-1): yderiv[i] = y(i+1] - y[i] Note that here, yderiv is one element shorter than y -- this is because you need 2 points...

  • Python, given a code in class i just need help with the third bullet point ; using a and n (defin...

    Python, given a code in class i just need help with the third bullet point ; using a and n (defined in the second picture of python code) find the first digit for k! for k =1,...,n. you dont have to type in all this code just help me write the code in the first picture where it says: def benford(a): b = [0 for d in range(1,10)] #Do everthything in here return b 2.2 Generating data In this assignment...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT