Teaching a half semester of scientific computing in one class period. I have been working on leaning into AI with how I teach. Students do it.
So here is how I would teach a student how to use AI to solve two problems that an upper-level student might face in their lab course. The problems (1) reading a data file and fitting it and (2) solving a standard 2nd order differential equation.
Let’s Go!
Step 1: Open Python. I like Spyder:

The frame on the left is place for the python script (e.g., code). Save this as a python file. That means is should end with a “.py”
Step 2: Go to Gemini or your favorite AI tool.
Step 3: Create a CSV file.
I created a CSV file called SimpleData.csv. The data is really simple.
Position,Force
1,1.1
2,1.9
3,3.5
4,4.2
5,4.5
6,5.1
7,7.3
8,8.5
9,8.9
10,10.1
CSV stands for comma Separated File
Step 4: Open Data in Python
Go to Gemini and ask:
Please generate python script that reads a CSV file with two columns ignoring the header row and plot that data in those columns
Then take the code it generated and put it into Python

Almost perfect! Go Gemini!
There are some changes that need to be made. And some explanations.
- Lines 1 & 2: These are libraries Python uses.
- Lines 4 & 5: “#” the text after this is grey. Python knows to ignore this.
- Line 7: replace ‘your_file.csv’ with ‘SimpleData.csv’
- Line 15 & 16: Converts the data “df” into to variables x and y. The “:” means use the whole column. The “0” means use the first column. The “1” means use the second column.
- Line 6, 10, 11,12, > 27: Gemini is too cute with its code. All of this is protection to make the code run nicely if there is something unexpected. Since it is our code, lets just make it more basic.
Now we have:

Step 5: Now let’s explore some plotting examples:
Go to this website and make some changes to your code based off of these examples.
https://matplotlib.org/stable/tutorials/pyplot.html

Step 6: Fit the Data
Let’s go back to Gemini:
Thank you Gemini. I would like to add a linear fit of the data to plot and print the slope and intercept
It generated new code. Let’s break it down:
import pandas as pd
import matplotlib.pyplot as plt
from scipy.stats import linregress
import numpy as np
New libraries Python needs. You can use “import” or “from” to access the libraries. They are different but similar.
# Perform linear regression to get the slope and intercept
slope, intercept, r_value, p_value, std_err = linregress(x_data,y_data)
# Print the slope and intercept
print(f"Slope: {slope:.4f}")
print(f"Intercept: {intercept:.4f}")
# Create the fitted line using the calculated slope and intercept
fitted_line = slope * x_data + intercept
This is fitting part. Let’s add it to the code. Note I am going to change x_data to x for ease of reading.
But OH NO ERROR!

So I went back to Gemini and asked
I got the following error "TypeError: ufunc 'true_divide' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''"
And it said:
It looks like the TypeError you're seeing is because the data from your CSV file isn't being read as numerical values, which are required for plotting and linear regression. This often happens when there are non-numeric characters in the CSV, such as a space, a text label, or a missing value represented by something other than NaN. Python's numerical operations, like ufunc 'true_divide', can't work on these non-numerical types.
To fix this, you need to ensure the data is properly converted to a numerical format. A robust way to do this is to use the pd.to_numeric() function with the errors='coerce' argument, which will convert any problematic values to NaN (Not a Number), allowing the rest of the data to be processed correctly.
So I did what it suggested.

I added the lines 8 and 9 to handle the convert to double. But now something weird happened when I ran the code. I got a nan (no not the tasty bread) for the slope. So I added a print statement in line 11 to see what was going on. And it was messing up the first row.
Oh I went back and read gemini’s response. I guess I missed a line:
# Drop any rows that now contain NaN values
df.dropna(inplace=True)
So it worked!

But I forgot to add the plot of the fit. Let’s add it

Here’s my plot:

Step 7: Uncertainity
In addition to getting the slope you also get the error in the slope. How awesome is that?
To Gemini:
How do you get the uncertainity in the slope and the interncept from using linregress in python
It said
The scipy.stats.linregress function does not directly return the uncertainties (standard errors) for the slope and intercept. However, it does return the standard error of the slope (std_err), which you can use along with other values to calculate the standard error of the intercept. The standard error of the slope is a direct output of the function.
To get the uncertainty in the slope and intercept, you must use a different function like scipy.optimize.curve_fit, which returns the covariance matrix. From this matrix, you can calculate the standard errors for both the slope and the intercept.
A common approach for a simple linear fit is to use linregress for the slope and then calculate the intercept's standard error using the standard error of the slope, the data's standard deviations, and the correlation coefficient. However, the most direct and robust method is to use curve_fit.
So if I want the error of the intercept I will need to think harder. But It does give me the error in the slope in std_err.
Let’s try something else. Let’s solve a differential equational:
To Gemini:
Gemini imagine that I have pendulum that obeys the following differential equation: the second derivative of the angle in time is equal to negative omega squared times the sine of that angle. Write a script in Python that solves this differential equation with the following two initial conditions the initial angle is 2 rads and the initial angular velocity is 0.
It generates some useful stuff right off the bat:

It also generated code. Let’s create a new blank script to run it in:

I pasted it in, ran it, and got an error. That stinks! But when I read the error it is some mumble jumble about “OSError: ‘seaborn-v0_8-whitegrid’ not found in the style library”. Who cares about style in the initial programming phase. I searched for ‘seaborn-v0_8-whitegrid’ and deleted those lines.
And it worked!

And here is the stuff after line 36

Let’s go through the code and what it does.
- Lines 1-3 add libraries
- Line 14 Tricky – Python solves this 2nd order DQ but converting it to 2 first order DQs. So each one needs an initial condition. So y0 contains both.
- Line 23 – 34 – contains both first order differential equations. One for the angle and the other for the angular velocity. It returns the updated angular velocity and angular acceleration.
- Line 40 – 49 – Solves the DQ – the code here is weird. I struggled understanding it.
- Line 53 – 70 – Prints the results. Notice sol_eval[0] is theta and sol_eval[1] is the angular velocity.

Now that this code works for a pendulum it will work for any 2nd order differential equation. You just need to make some stragetic edits.





Leave a Reply