-1

The question and my problem is as follows;

define a python function f(x) = ln(x) + x^2 - 1 and print the output for f(1.00001), f(0.5) and f(10^-10)

I can print the output for each of these separately but can't produce it so that I can print the 3 outputs in the same cell without producing 3 separate cells with the 3 values of x.

My code is currently as follows:

import numpy as np 
def f(x):
    y = np.log(x) + x**2 - 1
    return y 
x = 0.5
f(x)
cigien
  • 57,834
  • 11
  • 73
  • 112
CJ1912
  • 3
  • 3

2 Answers2

0

In Jupyter Notebook, you should be able to print out any number of print statements in a sequence and they will all appear in one output cell:enter image description here

dsillman2000
  • 976
  • 1
  • 8
  • 20
0

Great question, there are three possible answers to this question in my mind.

  1. You could give the function addresses (or pass by reference) of variable which you then set in the function
  2. You could create a Tuple class that holds multiple values, initialize the class with a temporary object, set the values you wish to return to the object, and return the object.
  3. Finally, I think the simplest solution would be to return multiple values in one return statement, (supported in python, not supported in C++), this is the best solution in my opinion because if the function output is printed, it prints all three returns as if they are in a three dimensional graph. (x, y, z). Here is some example code...

def f():
    val1 = 1
    val2 = 2.7
    val3 = "happy"
    return val1, val2, val3

num1 = f()
print(num1)

The printout would be: " (1, 2.7, 'happy') " this example shows that the return type doesn't have to be the same for all of the elements

Jacob Glik
  • 223
  • 3
  • 10