Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have a code to find the electric field and the potential. i want to re write it as class but in easier way The

I have a code to find the electric field and the potential. i want to re write it as class but in easier way

The code:

class ChargedParticle: def __init__(self, pos, charge): self.pos = np.asarray(pos) self.charge = charge def compute_field(self, x, y): # this part i want to rewrite it X, Y = np.meshgrid(x, y) u_i = np.hstack((X.ravel()[:, np.newaxis], Y.ravel()[:, np.newaxis])) - self.pos r = np.sqrt((X - self.pos[0]) ** 2 + (Y - self.pos[1]) ** 2) field = ((self.charge / r ** 2).ravel()[:, np.newaxis] * u_i).reshape(X.shape + (2,)) return field def compute_potential(self, x, y): X, Y = np.meshgrid(x, y) r = np.sqrt((X - self.pos[0]) ** 2 + (Y - self.pos[1]) ** 2) potential = self.charge / r return potential

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

A Survey of Mathematics with Applications

Authors: Allen R. Angel, Christine D. Abbott, Dennis Runde

10th edition

134112105, 134112342, 9780134112343, 9780134112268, 134112261, 978-0134112107

Students also viewed these Physics questions