[Tutor] Fsolve and CSV help

Matt Lester mattlester1 at u.boisestate.edu
Wed Oct 13 11:40:42 EDT 2021


Hi everyone,

I am trying to use the fsolve function to solve two equations for two variables (theta1 and theta2). The rest of the variables in the equation are either constants or are given in a CSV file. I have tried the code several ways, but can’t seem to get it right (the most recent code is below). I am struggling with:

	• Where do i read in and update the CSV data? Inside or outside the function?
	• Where to place the fsolve? Again, inside or outside the function?
	• I am having some issues with the array sizes, I’m assuming this has to do with the CSV data being larger than the array of the function, but I’m not sure how to fix that.

I am new to python, and coding in general, so I apologize if I’m using incorrect terminology or my code is totally off base.

I’ve added a sample of the data given in the CSV, for this problem I am only using the ‘x’ and ‘y’ columns.

Thanks for any help or advice you can give me!!!

Matt



-------------- next part --------------


import pandas as pd
import numpy as np
from scipy.optimize import fsolve


def Ik(thetas):
    df = pd.read_csv("ik_question.csv")         # reads in the CSV file info

    x = df['x'].values
    y = df['y'].values
    L1 = 1
    L2 = 1
    L3 = 0.5
    theta1 = thetas[0]
    theta2 = thetas[1]
    theta3 = np.pi/3

# equations for theta 1 and theta2
    thetaEQ = np.array([2,11], dtype=np.float64)
    
    thetaEQ[0] = L3*np.cos(theta1 + theta2 + theta3) + L2*np.cos(theta1 + theta2) + L1*np.cos(theta1) - x
    thetaEQ[1] = L3*np.sin(theta1 + theta2 + theta3) + L2*np.sin(theta1 + theta2) + L1*np.sin(theta1) - y
    
    thetas = fsolve(Ik, (0,0))
    df.insert(2,"th1",theta1)
    df.insert(3,"th2",theta2)
    df.insert(4,"th2",theta3)
    df.to_csv('ik_questions1.csv')
  
    
    return thetaEQ


More information about the Tutor mailing list