Help me please!
Need c# code...

import numpy
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.pyplot as plt

def zadachaOPrepyatstvii(h, f, fi, xi, sigma, epsilon=0.0000001):
 m = int(1/h)
 h2 = h * h
 u = numpy.zeros(shape=(m+1, m+1))
 gamma = numpy.zeros(shape=(m+1, m+1))
 prevnormr = 0
 while True:
 for i in xrange(1, m):
 for j in xrange(1, m):
 x = (i * h, j * h)
 v = (1 - sigma) * u[i][j] + sigma * 0.25 * (u[i-1][j] + u[i+1][j] + u[i][j-1] + u[i][j+1] + h2 * f(i*h, j*h))
 fi_x = fi(x)
 if v < fi_x:
 u[i][j] = fi_x
 else:
 xi_x = xi(x)
 u[i][j] = xi_x if v > xi_x else v
 gamma[i][j] = 1 / (h2 * sigma) * (v - u[i][j])
 norm_r = 0
 for i in xrange(1, m):
 for j in xrange(1, m):
 r = 1 / h2 * (4 * u[i][j] - u[i-1][j] - u[i+1][j] - u[i][j-1] - u[i][j+1]) + gamma[i][j] - f(i*h,j*h)
 norm_r += h2 * r * r
 print pow(norm_r, 0.5)
 if abs(norm_r - prevnormr) < epsilon:
 break
 prevnormr = norm_r
 return u


def drawPlot(X, Y, Z, minZ=-1.01, maxZ=2.01):
 fig = plt.figure()
 ax = fig.gca(projection='3d')
 X, Y = numpy.meshgrid(X, Y) surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=0, antialiased=False)
 ax.set_zlim(minZ, maxZ)
 ax.zaxis.set_major_locator(LinearLocator(10))
 ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
 fig.colorbar(surf, shrink=0.5, aspect=5)
 plt.show()


def loadMatrix(fname):
 t = open(fname, "rt").readlines()
 u = numpy.zeros(shape=(len(t), len(t[0].split(";"))))
 for i, line in enumerate(t):
 for j, val in enumerate(map(float, line.split(";"))):
 u[i][j] = val
 return u

def getAu(u, h):
 r = numpy.zeros(shape=u.shape)
 h2 = h * h
 for i in range(1, u.shape[0] - 1):
 for j in range(1, u.shape[0] - 1):
 r[i][j] = 1 / h2 * (4 * u[i][j] - u[i-1][j] - u[i+1][j] - u[i][j-1] - u[i][j+1])
 return r

def f(x, y):
 if x > 0.3 and x < 0.7 and y > 0.3 and y < 0.7:
 return -60
 return 40

h = 0.02
res = zadachaOPrepyatstvii(h, f, lambda x: 0, lambda x: 10000000.7, 1.9, 0.0001)
drawPlot(numpy.arange(0, 1 + h, h), numpy.arange(0, 1 + h, h), res)

moderation: please use the editor's code feature for syntax highlighting.

Recommended Answers

All 2 Replies

Hi belladiluna, welcome here at DaniWeb!
Because for the most part, you just have formulas, this should be relatively easily translated to c#. Also remember that c# can make use of charting, via a .NET Chart class

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.