Skip to Main Content Skip to Search
Product Documentation

Bouncing Ball Simulation Example

Example Overview

This example is adapted from the ballode demo provided with the MATLAB product. It demonstrates repeated event location, where the conditions are changed after each terminal event.

This demo computes 10 bounces with calls to ode23, which is a MATLAB function. A user-specified damping factor after each bounce attenuates the speed of the ball. The trajectory is plotted using the output function odeplot. In addition to the damping factor, the user can also provide the initial velocity, the maximum number of bounce to track, and the maximum time until demo is completed.

This example shows you how to create the COM component using the MATLAB Builder NE product and how to use this COM component in a Visual Basic application independent of MATLAB.

Building the Component

  1. At the MATLAB command prompt, change folders to matlabroot/BallODE.

  2. Enter the command deploytool to open the Deployment Tool window.

  3. Use the Deployment Tool to create a project with the following settings:

    SettingValue
    Project namebouncingBall
    Class namebouncingBallclass
    Project folderThe name of your work folder followed by the component name
    Generate Verbose OutputSelected

  4. Locate your work folder, navigate to matlabroot/BallODE, and add ballode.m to the project.

  5. Build the component by clicking the button in the Deployment Tool toolbar.

    The build process begins, and a log of the build appears in the Output pane of the Deployment Tool window. The files that are needed for the component are copied to two newly created folders, src and distrib, in the bouncingBall folder. A copy of the build log is placed in the src folder.

Using the Component in Microsoft Visual Basic

You can call the component from any application that supports COM.

To create a Microsoft Visual Basic project and add references to the necessary libraries:

  1. Start Visual Basic.

  2. Create a new Standard EXE project.

  3. Select Project > References.

  4. Select the following libraries:

    • bouncingBall 1.0 Type Library

      (If you named your class something other than bouncingBall or gave a different version number, click and use the appropriate component and corresponding type library.)

    • MWComUtil 7.5 Type Library

        Note   If you do not see these libraries, you may not have registered the libraries using mwregsvr. Refer to Component Registration for information on this.

Creating the Microsoft Visual Basic Form

The next task is to create a front end or a Microsoft Visual Basic form for the application. End users enter data with this form.

To create a new user form and populate it with the necessary controls:

  1. Select Projects > Component. Alternatively, press Ctrl+T.

  2. Check that Microsoft Windows Common Controls 6.0 is selected. You will use the ListView control from this component library.

  3. Add a series of controls to the blank form to create an interface with the properties listed in the following table.

    Control TypeControl NamePropertiesPurpose

    Form

    frmBallOde

    Caption = Bouncing Ball ODE

    Container for all components.

    Frame

    frmInput

    Name = frmInput*

    Caption = Input Data Points

    Groups all input controls.

    Frame

    frmOutput

    Name = frmOutput*

    Caption = Output Coefficients

    Groups all output controls.

    Label

    lblInitVal

    Caption = Initial Velocity

    Labels the text box txtInitVal.

    TextBox

    txtInitVal

    Text =

    Holds initial velocity by which ball is thrown into the air.

    Label

    lblDamp

    Caption = Damping Factor

    Labels the text box txtDamp.

    TextBox

    txtDamp

    Text =

    Holds damping factor for the bounce, that is, the factor by which the speed of the ball is reduced after it bounces.

    Label

    lblIter

    Caption = Number of Bounces

    Labels the text box txtIter.

    TextBox

    txtIter

    Text =

    Holds number of iterations or bounces to track.

    Label

    lblFinalTime

    Caption = Maximum Time

    Labels the text box txtFinalTime.

    TextBox

    txtFinalTime

    Text =

    Stores time until demo is completed.

    ListView

    lstBounce

    Name = lstBounce

    GridLines = True

    LabelEdit = lvwManual

    View = lvwReport

    Displays the time stamp when ball bounces off the ground.

    CommandButton

    cmdEvaluate

    Caption = Evaluate

    Default = True

    Executes the function.

    CommandButton

    cmdCancel

    Caption = Cancel

    Cancel = True

    Closes the dialog box without executing the function.

  4. When the design is complete, save the project by selecting File > Save. When prompted for the project name, type BallOde.vbp, and for the form, type frmBallOde.frm.

  5. In the Project dialog box, right-click frmBallOde and select View Code.

    The following code listing shows the code to implement. Note that this code references the control and variable names listed above. If you have given a different name to any of the controls or any global variable, change this code to reflect the differences.

    Private theBall As Variant ' Variable to hold the COM object.
    
    Private Sub cmdCancel_Click()
        ' If the user presses the Cancel button, unload the form.
        Unload Me
    End Sub
    
    Private Sub Form_Initialize()
        Dim Len1 As Long ' Used to set length of columns for list box.
        Dim Len2 As Long ' Used to set length of columns for list box.
        On Error GoTo Handle_Error
        ' Set length of the each column based on length of the listbox
        ' such that the two columns span the maximum area without 
        ' creating a horizontal scroll bar.
        Len2 = lstBounce.Width / 2
        Len1 = (lstBounce.Width - Len2) - 300
        
        ' Add column headers to each column in the list box.
        Call lstBounce.ColumnHeaders.Add(, , "Bounce", Len1)
        Call lstBounce.ColumnHeaders.Add(, , "Time", Len2)
        
        ' Set tab indices for each component.
        txtInitVel.TabIndex = 1
        txtDamp.TabIndex = 2
        txtIter.TabIndex = 3
        txtFinalTime.TabIndex = 4
        cmdEvaluate.TabIndex = 5
        cmdCancel.TabIndex = 6
        lstBounce.TabStop = False
        
        ' Create the COM object
        ' If there is an error, handle it accordingly.
        Set theBall = CreateObject("bouncingBall.bouncingBall.1_0")
        Exit Sub
    Handle_Error:
        ' Error handling code
        MsgBox ("Error " & Err.Description)
    End Sub
    Private Sub cmdEvaluate_Click()
        ' Dim R As Range
        Dim zeroTime As Variant ' Result variable object.
        Dim loopCount As Integer
        Dim item As ListItem
        
        ' Check if the object was created properly.
        ' If not, go to the error handling routine.
        If theBall Is Nothing Then GoTo Exit_Form
        
        ' If there is an error, continue with the code.
        On Error Resume Next
        
        ' Process inputs
        ' If the user does not provide the values for input parameters,
        ' use the default values.
        If txtDamp.Text = Empty Then
            txtDamp.Text = 0.9
        End If
        If txtInitVel.Text = Empty Then
            txtInitVel.Text = 20
        End If
        If txtIter.Text = Empty Then
            txtIter.Text = 15
        End If
        If txtFinalTime.Text = Empty Then
            txtFinalTime.Text = 20
        End If
            
        'Compute Bouncing ball data
        Call theBall.ballode(1, zeroTime, CDbl(txtIter.Text),_ 
        CDbl(txtDamp.Text), CDbl(txtFinalTime.Text),_ 
        CDbl(txtInitVel.Text))
        
        ' Display the output values (time stamp when ball bounces on 
        ' the ground).
        Call lstBounce.ListItems.Clear
        For loopCount = LBound(zeroTime, 1) To UBound(zeroTime, 1)
            Set item = lstBounce.ListItems.Add(, , Format(loopCount))
            Call item.ListSubItems.Add(, , Format(zeroTime(loopCount,_ 
            1), "##.###"))
        Next
        Call lstBounce.Refresh
        
        GoTo Exit_Form
    Handle_Error:
        ' Error handling routine
        MsgBox (Err.Description)
    Exit_Form:
    End Sub
    

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS