PHYSICS 2235

Lab 08

#! /usr/local/bin/python3 import numpy as np import matplotlib.pyplot as plt ######################### def func(x): """Evaluate a function""" return x**2 - np.sin(x) ######################### x1 = np.linspace(0.1,10, 256) plt.plot(x1,func(x1),'k-') plt.show()explaining code

Write a function that prints the statement "this function.." without it taking any arguments.

The variables created inside a function cannot be seen by the program itself. This is an important feature of writing a function. This means that you can create variables and arrays inside the function. Call them whatever you please and it would not be recognized or affect your main code. The only time you have access to a variable in a function is when you use the **return** statement.

import numpy as np import matplotlib.pyplot as plt def func(x): a = x**2 b = 1/x c = a * b return c var1 = 5.5 print(func(var1)) print(a) print(b) print(z)

import numpy as np import matplotlib.pyplot as plt def func(x): a = x**2 b = 1/x c = a * b return c var1 = 5.5 print(func(var1)) #Your input variable/array can be called x or anything else x = np.linspace(0.1,10, 256) v = np.linspace(0.1,10, 256) w = np.linspace(0.1,10, 256)

import numpy as np import matplotlib.pyplot as plt def xf(keyOpt,n): if(keyOpt=='Yes'): x= np.linspace(0, 180, n) y= np.linspace(0, 580, n) if(keyOpt=='No'): x= np.linspace(0, 18, n) y= np.linspace(0, 58, n) return x,y x1,y1 = xf('yes',150) print (x1,y1)explaining code

Note that there is a bug in your code. Find it and fix it.

Hint: Note that the string is case sensetive.

import numpy as np import matplotlib.pyplot as plt def xf(keyOpt,n): if(keyOpt=='Yes'): x= np.linspace(0, 180, n) y= np.linspace(0, 580, n) if(keyOpt=='No'): x= np.linspace(0, 18, n) y= np.linspace(0, 58, n) print(x) print(y) plt.plot(y,x,'b--') plt.savefig('line.png') plt.show() xf('Yes',150)explaining code

import numpy as np import matplotlib.pyplot as plt def funcOpt(r, n=12): theta = np.pi/4; x = r * np.cos(theta) return x**n a = funcOpt(3) b = funcOpt(3, n=6) c = funcOpt(3, n=4)explaining code

numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)

which ones is the

The following **absolute_value** function works fine when the argument is a single variable.

def absolute_value(n): if n < 0: n = -n return n x=-50 y=absolute_value(x) print (y)

Note here that the

x = np.linspace(-10, 10, 10) y=absolute_value(x) print (y)In the main script. Your code will return an error. Try it.

One way to solve the problem is to have a for loop. For example:

def absolute_value(n): y = np.zeros(n.size) for i in range(n.size):# loops over all elements in array if n[i] < 0: y[i] = -1*n[i] else: y[i] = n[i] return y

x = np.linspace(-10, 10, 10) y=absolute_value(x) print(y)Here the for loop evaluates the elements in the x array one by one and append the result. When its finished it will return an array.

Now try to use this last code for a single variable. Add the lines

x1=-50 y1=absolute_value(x1)Note that it does not work for a single variable anymore.

Here we can use the

From the above example:where(condition, output if True, output if False)

def simple_absolute(x): z = np.where(x<0.0, -1*x, x) return zNow try to use this function for array and single variable.

You are not required to reproduce the face in these examples exactly. In fact, have fun with the face and get creative if you like. However, you need to submit a face with eyes, mouth, and nose.

\mathrm{sinc}\,x = \frac{\sin x}{x}
= \frac{x - \frac{x^3}{3!} + \frac{x^5}{5!} + ...}{x}
= 1 - \frac{x^2}{3!} + \frac{x^4}{5!} + ... \;.

From the Taylor series it is clear that when x=0 sinc(x)=1. Make sure that your function behaves properly around x = 0.

Back to main Physics 2235 page