Scientific Computing: Method of Undetermined Coefficients in Python

Click For Summary

Discussion Overview

The discussion centers around translating MATLAB code from a specific book on scientific computing into Python, focusing on the method of undetermined coefficients. Participants share their experiences and seek verification of results obtained from their Python implementations.

Discussion Character

  • Exploratory
  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant expresses a desire to translate MATLAB code from a book into Python due to the cost of MATLAB.
  • Another participant suggests that the MATLAB script can be directly adapted into Python by copying and modifying it line by line, providing an example of how to define inputs in Python.
  • A participant shares their results from a Python implementation, asking for verification from someone with MATLAB access.
  • Another participant mentions the use of Wolfram Alpha as an alternative for verification of results.
  • A different participant recommends using GNU Octave as a free alternative to MATLAB, noting its effectiveness and recent updates.

Areas of Agreement / Disagreement

Participants generally agree on the feasibility of translating MATLAB code to Python, but there is no consensus on the verification of the results obtained, as some seek confirmation while others suggest alternative tools.

Contextual Notes

Some participants mention specific programming functions and libraries, indicating that familiarity with Python and its libraries may be necessary for successful translation and implementation.

Who May Find This Useful

Individuals interested in scientific computing, programming in Python, or those looking for alternatives to MATLAB may find this discussion beneficial.

the_dane
Messages
29
Reaction score
0
TL;DR
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
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
 
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!
 
the_dane said:
Can someone with MATLAB license please verify this result. I will be extremely helpfull, thanks!
You can use Wolfram Alpha.
 
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   Reactions: jim mcnamara and Stephen Tashi

Similar threads

  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K