Scientific Computing: Method of Undetermined Coefficients in Python

In summary, the person looking for help translating the code from the self-learning book into Python found a website that provides translated code.
  • #1
the_dane
30
0
TL;DR Summary
I am asking if anyone have some Python code to compute coefficients
In a self learning project I am fooling around book https://faculty.washington.edu/rjl/fdmbook/

I want to do some of the computation myself to better understand the concepts but the book is Matlab based and Matlab is too expensive.
Does anyone by any chance have some of the codes provided by the book translated into Python?

Specifically I am looking at this function here which I want in Python:
https://faculty.washington.edu/rjl/fdmbook/matlab/fdcoeffF.m
 
Technology news on Phys.org
  • #2
You can almost literally use that MATLAB script in python. Just copy the entire contents of the function into e.g. a jupyter notebook cell and run it (not as a function but just the contents of the function). Then start at line 1 and fix it line by line into python code.

For instance, the first line is n=length(x), so before this line you start by defining the inputs. Define an array of coordinates called x:
Python:
import numpy as np
x = np.array([0.0, 0.1, 0.2])

and then you find out that the command for length() is called len() in python
Python:
n = len(x)

etc. It is a very basic code, so some self-learning of python and numpy for an hour should give you enough background to finish the porting to python. You can start here and follow the basic python tutorial and then the numpy tutorial:

https://www.w3schools.com/python/python_syntax.asp
 
  • #3
I have now typed something in Python.
for k = 2, xbar = pi/2, and x =[pi/2-0.2 , pi/2-0.1, pi/2, pi/2 + 0.1, pi/2 + 0.2]

I get the results:

C = [-8.33333333, 133.33333333, -250. , 133.33333333, -8.33333333]Can someone with MATLAB license please verify this result. I will be extremely helpfull, thanks!
 
  • #4
the_dane said:
Can someone with MATLAB license please verify this result. I will be extremely helpfull, thanks!
You can use Wolfram Alpha.
 
  • #5
Octave, https://www.gnu.org/software/octave/index, is a freeware Matlab clone. I have been using it for several years, and it has worked very well. The only real difference is that many of the help files are a little less helpful than those in Matlab.

I see that version 6.2 has been released. Time to upgrade.
 
  • Like
Likes jim mcnamara and Stephen Tashi
  • #6

1. What is the method of undetermined coefficients in Python?

The method of undetermined coefficients is a numerical technique used in scientific computing to solve differential equations. It involves finding a particular solution to a nonhomogeneous differential equation by assuming a form for the solution and solving for the coefficients.

2. How is the method of undetermined coefficients implemented in Python?

The method of undetermined coefficients can be implemented in Python using the SymPy library, which provides tools for symbolic mathematics. The functions solve_undetermined_coeffs and undetermined_coeffs can be used to solve for the coefficients of a particular solution to a differential equation.

3. What types of differential equations can be solved using the method of undetermined coefficients?

The method of undetermined coefficients is typically used for linear, nonhomogeneous differential equations with constant coefficients. This includes equations such as second-order ordinary differential equations, as well as systems of linear differential equations.

4. What are the advantages of using the method of undetermined coefficients?

The method of undetermined coefficients offers a straightforward and efficient way to find particular solutions to nonhomogeneous differential equations. It also does not require extensive knowledge of advanced mathematical concepts, making it accessible to a wide range of users.

5. Are there any limitations to using the method of undetermined coefficients?

While the method of undetermined coefficients is a useful tool for solving certain types of differential equations, it does have some limitations. It may not work for all types of equations, and it may not always provide the most accurate solution. Additionally, it can become more complex and time-consuming for higher-order equations.

Similar threads

  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
5
Views
994
  • Programming and Computer Science
Replies
1
Views
777
  • Programming and Computer Science
Replies
3
Views
320
  • Programming and Computer Science
Replies
9
Views
2K
Replies
6
Views
1K
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
Back
Top