Consulting

Results 1 to 6 of 6

Thread: Polynom calculation. Help. My code doesn't work

  1. #1
    VBAX Newbie
    Joined
    May 2021
    Posts
    2
    Location

    Exclamation Polynom calculation. Help. My code doesn't work


    1. 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]

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,704
    Location
    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
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Posts
    7,950
    Location
    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
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    https://www.dropbox.com/s/w0grjgt438...mial.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.

  5. #5
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,704
    Location
    FYI, using the Go Advanced button will let you upload Workbooks here as attachments to your post.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  6. #6
    no matter how much i tried, it won't Attached my .xlsm or .zip file.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •