PDA

View Full Version : Polynom calculation. Help. My code doesn't work

dontlook
05-28-2021, 03:50 AM
Write a Public Function named “FunctionValues”, which takes values of vector x from the specified cells (array) and calculate the function f(x) = x^2 + 1/2x + 2 for all values (array). The function should have one parameter (type array) and will return one array with the same dimension as parameter. Provide some example of using such a function in the worksheet. Please note that it should be programmed as a function and not subroutine!!!

Dim Row As IntegerDim Column As Integer
MyArray = Range("A1:C3")
Row = InputBox(Prompt:="Row")
Column = InputBox(Prompt:="Column")
If Row < 3 And Column < 3 Then
MsgBox "Item on row " & Row & " and column " & Column & " is " & MyArray(Row, Column)
Else
MsgBox "There is no such item"
End If
End Sub

Public Function Polyn(Rin As Range) As Double
Dim v As Variant
v = Rin.Value
Polyn = x ^ 2 + 1 / 2 * x + 2
End Function

SamT
05-28-2021, 10:11 AM
HomeWork... bleh!
There is no "Array" Type, Use the "Variant" Type to hold Arrays
Polynomials work on numbers, not Ranges (Cells). To set an array to cell values,

Dim Myarray as Variant
MyArray = Range("A1:C3").Value
To pass an Array to a Function that will return an array

Function Polys(Input As Variant) As Variant
Since the math only works on one value at a time, you will need to iterate thru each Value in the (2d)array

Dim Rw as Long, Cl As Long
For Rw = Lbound(Array, 1) to Ubound(array, 1)
For Cl = Lbound(Array, 2) to Ubound(array, 2)
x = Array(Rw, Cl)
xx = (x ^ 2 + 1 / 2 * x + 2)
Next
Next
You also need an array to hold the result of the math, It must be the same size (dimensions) as the input array

Dim Result As Variant
Redim Result(Ubound(Input, 1),Ubound(Input, 2))
In Code

For Rw = Lbound(Array 1) to Ubound(array 1)
For Cl = Lbound(Array 2) to Ubound(array 2)
x = Array(Rw, Cl)
Result(Rw, Cl) = (x ^ 2 + 1 / 2 * x + 2)
Finally, after all that, set the Function = Result

'
'
Next
Next
FunctionValues = Result
End Function

Your code should handle when only one value is passed to the Function

Function Polys(...)...
If IsArray(Input) then
'Do all the above coding
Else
Result = (Input^ 2 + 1 / 2 * Input + 2
End If
FunctionValues = Result
End Function

Paul_Hossler
05-29-2021, 06:25 AM
The output area for the function should be array entered, that is control+shift+enter

Option Explicit
'Write a Public Function named “FunctionValues”, which takes values of vector x from the specified cells (array) and
'calculate the function f(x) = x^2 + 1/2x + 2 for all values (array).
'The function should have one parameter (type array) and will return one array with the same dimension as parameter.
'Provide some example of using such a function in the worksheet.
'Please note that it should be programmed as a function and not subroutine!!!

Public Function FunctionValues(aryIn As Variant) As Variant
Dim aryOut(1 To 3, 1 To 3) As Double
Dim r As Long, c As Long

For r = 1 To 3
For c = 1 To 3
aryOut(r, c) = aryIn(r, c) ^ 2 + (1 / 2) * aryIn(r, c) ^ 2 + 2
Next c
Next r

FunctionValues = aryOut

End Function

arnelgp
07-17-2021, 05:53 AM
https://www.dropbox.com/s/w0grjgt4380vr04/polynomial.xlsm?dl=0
on the Home tab there is a Group (polynomial) to the right, click
and highlight A1 to A3.

on the second texbox, click on any cell as output.

SamT
07-17-2021, 08:20 AM
FYI, using the Go Advanced button will let you upload Workbooks here as attachments to your post.

arnelgp
07-17-2021, 08:26 PM
no matter how much i tried, it won't Attached my .xlsm or .zip file.