Hi guys, I'm new here.
I'm trying to write some code which will take a polynomial and output it's value for varying x.
For a given input "n" I contruct n+1 placeholders (1, x, x^2, ... , x^n)
Beneath them I put in values
Then for a given "x" (in cells (3,2)).
I run the following code to get a value for f(x) (which is put in cell (9,1)).
![]()
Function f_1(n, x) f_1 = x ^ n End Function
![]()
Sub making_f() x = Worksheets(2).Cells(3, 2).Value 'this is the input for x n = Worksheets(2).Cells(1, 2).Value 'this is the degree of the polynomial Worksheets(2).Cells(8, 2) = Worksheets(2).Cells(7, 2).Value 'this is dropping down the constant term to the next row For m = 1 To n Worksheets(2).Cells(8, 2 + m) = Worksheets(2).Cells(7, m + 2) * f_1(m, x) Next 'this is inputting the values of constant*x^m in the next row Worksheets(2).Cells(9, 2) = Worksheets(2).Cells(7, 2).Value 'this is dropping the constant term down again as I don't know how to sum efficiently in vba I do it iteratively. For m = 1 To n Worksheets(2).Cells(9, m + 2) = Worksheets(2).Cells(9, m + 1).Value + Worksheets(2).Cells(8, m + 2).Value 'this iterates the summation Next Worksheets(2).Cells(9, 1) = Worksheets(2).Cells(9, n + 2).Value 'this gives me the value of f(x) End Sub
Now the problem is that I want to make the function f.
I tried the following to no avail:
![]()
Function f(x) Worksheets(2).Cells(3, 2).Value = x making_f f = Worksheets(2).Cells(9, 1).Value End Function
I just get a value error.
Any ideas? P.S. Sorry if my formatting is awful, I'm new to presenting code on forums.
Bookmarks