NUMERICAL SIMULATIONS AND CASE STUDIES USING VISUAL C++ · PDF fileNUMERICAL SIMULATIONS AND...

30
NUMERICAL SIMULATIONS AND CASE STUDIES USING VISUAL C++.NET SHAHARUDDIN SALLEH Universiti Teknologi Malaysia ALBERT Y. ZOMAYA University of Sydney STEPHAN OLARIU Old Dominion University BAHROM SANUGI Universiti Teknologi Malaysia A JOHN WILEY & SONS, INC., PUBLICATION

Transcript of NUMERICAL SIMULATIONS AND CASE STUDIES USING VISUAL C++ · PDF fileNUMERICAL SIMULATIONS AND...

  • NUMERICAL SIMULATIONSAND CASE STUDIES USINGVISUAL C++.NET

    SHAHARUDDIN SALLEHUniversiti Teknologi Malaysia

    ALBERT Y. ZOMAYAUniversity of Sydney

    STEPHAN OLARIUOld Dominion University

    BAHROM SANUGIUniversiti Teknologi Malaysia

    A JOHN WILEY & SONS, INC., PUBLICATION

    ffirs.qxd 6/2/2005 4:45 PM Page iii

    Innodata0471727245.jpg

  • ffirs.qxd 6/2/2005 4:45 PM Page ii

  • NUMERICAL SIMULATIONS AND CASE STUDIES USING VISUAL C++.NET

    ffirs.qxd 6/2/2005 4:45 PM Page i

  • ffirs.qxd 6/2/2005 4:45 PM Page ii

  • NUMERICAL SIMULATIONSAND CASE STUDIES USINGVISUAL C++.NET

    SHAHARUDDIN SALLEHUniversiti Teknologi Malaysia

    ALBERT Y. ZOMAYAUniversity of Sydney

    STEPHAN OLARIUOld Dominion University

    BAHROM SANUGIUniversiti Teknologi Malaysia

    A JOHN WILEY & SONS, INC., PUBLICATION

    ffirs.qxd 6/2/2005 4:45 PM Page iii

  • Copyright 2005 by John Wiley & Sons, Inc. All rights reserved.

    Published by John Wiley & Sons, Inc., Hoboken, New Jersey.Published simultaneously in Canada.

    No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form orby any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except aspermitted under Section 107 or 108 of the 1976 United States Copyright Act, without either the priorwritten permission of the Publisher, or authorization through payment of the appropriate per-copy fee tothe Copyright Clearance Center, Inc., 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax(978) 646-8600, or on the web at www.copyright.com. Requests to the Publisher for permission shouldbe addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ

    Limit of Liability/Disclaimer of Warranty: While the publisher and author have used their best efforts inpreparing this book, they make no representation or warranties with respect to the accuracy orcompleteness of the contents of this book and specifically disclaim any implied warranties ofmerchantability or fitness for a particular purpose. No warranty may be created or extended by salesrepresentatives or written sales materials. The advice and strategies contained herein may not besuitable for your situation. You should consult with a professional where appropriate. Neither thepublisher nor author shall be liable for any loss of profit or any other commercial damages, includingbut not limited to special, incidental, consequential, or other damages.

    For general information on our other products and services please contact our Customer CareDepartment within the U.S. at 877-762-2974, outside the U.S. at 317-572-3993 or fax 317-572-4002.

    Wiley also publishes its books in a variety of electronic formats. Some content that appears in print,however, may not be available in electronic format. For more information about Wiley products, visitour web site at www.wiley.com.

    Library of Congress Cataloging-in-Publication Data is available.

    Numerical simulations and case studies using Visual C++.Net / ShaharuddinSalleh . . . [et al].

    p. cm.Includes bibliographical references.ISBN-10 0-471-69461-4 (cloth : alk. paper)ISBN-13 978 0-471-69461-8

    1. C++ (Computer program language) 2. Computer simulation. 3. Microsoft.NET. 4. Microsoft Visual C++. I. Salleh Shaharuddin. 1956

    QA76.76.C153N86 2005005.133--dc22 2004025688

    Printed in the United States of America.

    10 9 8 7 6 5 4 3 2 1

    ffirs.qxd 6/2/2005 4:45 PM Page iv

    07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permission.

    http://www.copyright.comhttp://www.wiley.com/go/permissionhttp://www.wiley.com

  • CONTENTS

    Preface xi

    1 Developing Applications Using Visual C++.Net 1

    1.1 Object-Oriented Approach to Visual C++.Net 1Managed Extension Features 2

    1.2 MFC Fundamental Features 3Graphics Device Interface 4GDI Functions 5Numerical Functions 6

    1.3 Writing Applications Using MFC 6Creating a New Project 7Creating a Window 8

    1.4 Writing the First Nonwizard Program 91.5 Discussion 16

    Windows Creation Process 161.6 Summary and Conclusion 17Bibliography 17

    2 Interfaces for Numerical Problems 19

    2.1 Visualizing a Numerical Problem 19The Art of Visualization 20

    2.2 Handling Arrays 22Dynamic Memory Allocation 23

    2.3 Finding the Root of a Nonlinear Equation 24Code2A: Bisection Iterative Method 26

    2.4 Solving a System of Linear Equations 31Code2B: Manual Approach to the SLE Problem 33Code2C: Resource File Approach for SLE 38

    2.5 Summary and Conclusion 48Bibliography 49

    v

    ftoc.qxd 6/2/2005 4:52 PM Page v

  • Code Listings 49Code2A: Bisection Method 49Code2B: Solving a System of Linear Equations 51Code2C: Resource File Approach to the SLE Problem 53

    3 Matrix Operations Using Wizard 57

    3.1 Document/View Architecture Using Wizard 573.2 Matrix Algebra 58

    Data Passing Between Functions 59Matrix Multiplication 60Finding the Inverse of a Matrix 61Code3A: Matrix Operations 63

    3.3 System of Linear Equations Problem Revisited 73Code3B: Solving the SLE Problem Using Wizard 74Code3B: Discussion 83

    3.4 Summary and Conclusion 84Bibliography 83Code Listings 84

    Code3AView: Matrix Operations 84Code3BView: System of Linear Equations 89

    4 Differential Equations Problems 93

    4.1 Differential Equations 934.2 Ordinary Differential Equations 94

    Fourth-order RungeKutta Method (RK4) 95Code4A: Small Window for Displaying Large Amounts of Data 96

    4.3 Partial Differential Equations 101Poisson Equation: Finite Difference Method 102Code4B: Solving the Poisson Equation 107

    4.4 Summary and Conclusion 114Bibliography 115Code Listings 115

    Code4A: RungeKutta Method for ODE 115Code4B: Poisson Equation Using the Finite Difference Method 118

    5 Drawing Curves 123

    5.1 Windows Graphics Representation 123Windows Coordinates System 124

    5.2 MFC Functions for Displaying Graphics 125Color Schemes 126Selecting an Object 127Filling a Rectangular Area with a Color 127Plotting a Point 128Drawing a Line 128Drawing an Object 129

    5.3 Drawing a Curve 129

    vi CONTENTS

    ftoc.qxd 6/2/2005 4:52 PM Page vi

  • Code5A: Mathematical Curves 130Drawing a Polynomial 134Drawing a Lemniscate 135Drawing Creative Net 137

    5.4 Cubic Spline Interpolation 139Code5B: Constructing a Cubic Spline 141

    5.5 Summary and Conclusion 147Bibliography 148Code Listings 148

    Code5A: Mathematical Curves 148Code5B: Natural Cubic Spline 152

    6 Working with Images 157

    6.1 Handling Images 1576.2 Bitmap File Format 158

    Raster Operations Involving Bit Shifting 159Code6A: Demonstrating Bit Shifting 161

    6.3 Edge-Detection Problem 166Sobel Filtering Method 168Laplacian Filtering Method 169Code6B: Detecting the Edges of an Image 170

    6.4 Summary and Conclusion 173Bibliographical Note 173Code Listings 173

    Code6A: Working with Colors 173Code6B: Edge Detection Problem 175

    7 Visualizing a Graph 179

    7.1 Elementary Graph Concepts 1797.2 Graph Visualization Model 183

    Code7A: Drawing a Graph 1837.3 Minimum Spanning Tree Problem 191

    Kruskals Algorithm 193Prims Algorithm 194Code7B: Visualizing the Minimum Spanning Tree 195

    7.4 Summary and Conclusion 203Bibliography 204Code Listings 204

    Code7A: Drawing a Graph 204Code7B: Minimum Spanning Tree 208

    8 Graph Applications 213

    8.1 GraphNetwork Relationship 2138.2 Shortest-Path Problem 214

    CONTENTS vii

    ftoc.qxd 6/2/2005 4:52 PM Page vii

  • Dijkstras Algorithm 215Thd FloydWarshall Algorithm 216Code8A: Shortest-Path Visualization 217

    8.3 Mesh Network Application 227Code8B: Shortest Path on a Mesh Network 229

    8.4 Summary and Conclusion 234Bibliography 235Code Listings 235

    Code8A: Shortest Path in a Graph 235Code8B: Shortest Paths in a Mesh Network 240

    9 Multiprocessor Scheduling Problem 247

    9.1 Parallel Computing Systems 2479.2 Task Scheduling Problem 249

    Task Scheduling Concepts 252Path Maximum Magnitude Scheduling Model 253

    9.3 Task Scheduling Visualization Model 2549.4 Summary and Conclusion 270Bibliography 271Code Listings 271

    Code9: Task Scheduling Using Four Processors 271

    10 Discrete-Event Simulation 281

    10.1 Concepts of Simulation 28110.2 Simulation Model Development 28210.3 Discrete-Event System Simulations 28310.4 Multicounter System with Blocking 286

    Our Scheduling Model 286Code10A: Simulating Multicounter Systems 288

    10.5 Queueing Systems 295M/M/1 Queueing System 295M/M/C Queueing System 297Code10B: Simulating the M/M/C System 299

    10.6 Summary and Conclusion 308Bibliography 309Code Listings 309

    Code10A: Discrete-Event Simulation with Blocking 309Code10B: M/M/C Queuing without Blocking 313

    11 Modeling Wireless Networks 321

    11.1 Wireless Cellular Networks 32111.2 Channel-Assignment Problem 32311.3 Channel Assignments: Discrete Model 325

    viii CONTENTS

    ftoc.qxd 6/2/2005 4:52 PM Page viii

  • Code11A: Channels for Mobile Users 32611.4 Solving the Channel-Assignment Problem 336

    Code11B: Solving the Channel-Assignment Problem 33611.5 Summary and Conclusion 342Bibliography 343Code Listings 343

    Code11A: Channel Assignments to Mobile Users 343Code11B: Channel-Assignment Problem 349

    Index 355

    CONTENTS ix

    ftoc.qxd 6/2/2005 4:52 PM Page ix

  • ftoc.qxd 6/2/2005 4:52 PM Page x

  • PREFACE

    Numerical computing has come a long way since the discovery of the first comput-er in the early 1940s. Computers have brought changes to the world through theircapability to handle and solve problems that