how to add sine and cosine in python code

Explaining what this post is about
I was recently online and providing help to a fellow python coder, and a query came up about how you would rewrite some code that had included the sine function  and the cosine function

We were asked to see if we could translate this code:

L_1=1;
L_2=1.5;
q_0=0.5;
q_1=pi()/4;
q_2=pi()/6;
%Position of the end effetor
x= q_0+L_1*cos(q_1)-L_2*cos(pi()-q_1-q_2)
y=L_1*sin(q_1)+L_2*sin(pi()-q_1-q_2)

into its python equivalent.

Some background about the output

In order to get the desired result, there is a need to import a package to provide some of the mathematical analysis required, and was achieved through using Numpy statistical analysis

This package allows the following functions to be used in the logic:

  • Pi
  • Cosine
  • Sine

And the result is

As a result, the below shows the output of the above question converted to its Python equivalent:

import numpy as np

a= np.pi
print("PI value is ", a)

L_1=1
L_2=1.5
q_0=0.5
q_1=a/4
q_2=a/6

print("L_1 value is",L_1)
print("L_2 value is",L_2)
print("q_0 value is",q_0)
print("q_1 value is",q_1)
print("q_2 value is",q_2)


x= (q_0+(L_1*(np.cos(q_1)))-(L_2*(np.cos(a-q_1-q_2))))
y= (L_1*(np.sin(q_1))+(L_2*(np.sin(a-q_1-q_2))))

print("x value is " , x)
print("y value is " , y)

with its output showing:

PI value is  3.141592653589793
L_1 value is 1
L_2 value is 1.5
q_0 value is 0.5
q_1 value is 0.7853981633974483
q_2 value is 0.5235987755982988
0.7071067811865476
-0.25881904510252085
x value is  1.5953353488403288
y value is  2.15599552062015

 

What is the r programming language

The R Project for Statistical Computing is beneficial to anyone who needs a statistical analysis performed on a dataset.

The language is

  • Open source so that anyone can use it.
  • And can work cross-platform for any operating system in use.
  • R can work with other similar packages; an example been Python, which can execute within R.
  • As a result, you get the power of the statistical side of R and the wide variety of functionality of Python.

An introduction to R further

To get you started, we have introduced some fundamental functionality in this video, and give a tour around some of the screens that are visible as you work through your data analytics project. Some of the things you will see:

  • Creating variables
  • Addition of variables
  • Writing variables to a CSV file
  • We are saving variables to a txt file.
  • Ensuring no headers are in the file.
  • Loading data from a file and printing its contents to screen.

[embedyt] https://www.youtube.com/watch?v=uZWF1RCbXHU[/embedyt]

How we got this far

To get started and be able to write your first R program, a couple of steps as follows:

Both installs are free and well supported, easy to download, and easy to install.

Data Analytics Ireland