Scientific Computing: Method of Undetermined Coefficients in Python

  • #1
30
0
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
 

Answers and Replies

  • #2
bigfooted
Gold Member
617
134
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
30
0
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 licence please verify this result. I will be extremely helpfull, thanks!
 
  • #5
jrmichler
Mentor
1,549
1,752
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

Related Threads on Scientific Computing: Method of Undetermined Coefficients in Python

  • Last Post
2
Replies
29
Views
5K
  • Last Post
Replies
2
Views
2K
  • Last Post
Replies
2
Views
5K
  • Last Post
Replies
4
Views
2K
  • Last Post
Replies
1
Views
762
  • Last Post
Replies
3
Views
2K
Replies
1
Views
9K
  • Last Post
Replies
0
Views
1K
Replies
366
Views
69K
Replies
1
Views
2K
Top