Chapter 12, User-Defined Classes and Object-Oriented Programming Video Solutions, MATLAB Programming for Engineers | Numerade (2024)

Stephen J. Chapman

Chapter 12

User-Defined Classes and Object-Oriented Programming - all with Video Answers

Educators

Chapter Questions

Problem 1

Demonstrate that multiple copies of the Timer class of Example 12.1 can function independently without interfering with each other. Write a program that creates a random $50 \times 50$ set of simultaneous equations and then solves the equations. Create three Timer objects as follows: one to time the equation creation process, one to time the equation solution process, and one to time the entire process (creation plus solution). Show that the three objects are functioning independently without interfering with each other.

Check back soon!

Problem 2

Improve the Timer class of Example 12.1 so that it does not fail if it is timing objects whose durations run over midnight. To do this, you will need to use function datenum, which converts a date and time into a serial date number that represents the years since year zero, including fraction parts. To calculate the elapsed time, represent the start time and elapsed time as serial date numbers and subtract the two values. The result will be elapsed time in years, which then must be converted to seconds for use in the Timer class. Create a static method to convert a date number in years into a date number in seconds, and use that method to convert both the start time and elapsed time in your calculations.

Check back soon!

Problem 3

Improve the Timer class of Example 12.1 in a different way so that it does not fail if it is timing objects whose durations run over midnight. To do this, you should convert each date vector into a dateTime object and calculate the difference in time between the start time and the end time in seconds using a duration calculation.

Check back soon!

Problem 4

Create a handle class called PolarComplex containing a complex number represented in polar coordinates. The class should contain two properties called magnitude and angle, where angle is specified in radians (see Figure 12.13).
The class should include access methods to allow controlled access to the property values, as well as methods to add, subtract, multiply, and divide two Pol arComplex objects.

PolarComplex objects can be converted to rectangular form using the following equations:
$$
\begin{aligned}
c & =a+b i=z \angle \theta \\
a & =z \cos \theta \\
b & =z \sin \theta \\
z & =\sqrt{a^2+b^2} \\
\theta & =\tan ^{-1} \frac{b}{a}
\end{aligned}
$$

Complex numbers are best added and subtracted in rectangular form.
$$
\begin{aligned}
& c_1+c_2=\left(a_1+a_2\right)+\left(b_1+b_2\right) i \\
& c_1-c_2=\left(a_1-a_2\right)+\left(b_1-b_2\right) i
\end{aligned}
$$

Complex numbers are best multiplied and divided in polar form.
$$
\begin{aligned}
c_1 \times c_2 & =z_1 z_2 \angle \theta_1+\theta_2 \\
\frac{c_1}{c_2} & =\frac{z_1}{z_2} \angle \theta_1-\theta_2
\end{aligned}
$$

Create methods that add, subtract, multiply, and divide Pol arCompl ex numbers based on Equations (12.12) through (12.15), designing them so that two objects
( GRAPH CAN'T COPY )
can be manipulated with ordinary math symbols. Include static methods to convert back and forth from rectangular to polar form for use with these calculations.

Check back soon!

04:46
Problem 5

Three-Dimensional Vectors The study of the dynamics of objects in motion in three dimensions is an important area of engineering. In the study of dynamics, the position and velocity of objects, forces, torques, and so forth are usually represented by three-component vectors $\mathbf{v}=x \hat{\mathbf{i}}+y \hat{\mathbf{j}}+z \hat{\mathbf{k}}$, where the three components $(x, y, z)$ represent the projection of the vector $\mathbf{v}$ along the $x, y$, and $z$ axes, respectively, and $\hat{\mathbf{i}}, \hat{\mathbf{j}}$, and $\hat{\mathbf{k}}$ are the unit vectors along the $x, y$, and $z$ axes (see Figure 12.14). The solutions of many mechanical problems involve manipulating these vectors in specific ways.
The most common operations performed on these vectors are:
1. Addition. Two vectors are added together by separately adding their $x, y$, and $z$ components. If $\mathbf{v}_1=x_1 \hat{\mathbf{i}}+y_1 \hat{\mathbf{j}}+z_1 \hat{\mathbf{k}}$ and $\mathbf{v}_2=x_2 \hat{\mathbf{i}}+y_2 \hat{\mathbf{j}}+z_2 \hat{\mathbf{k}}$, then $\mathbf{v}_1+\mathbf{v}_2=\left(x_1+x_2\right) \hat{\mathbf{i}}+\left(y_1+y_2\right) \hat{\mathbf{j}}+\left(z_1+z_2\right) \hat{\mathbf{k}}$.
2. Subtraction. Two vectors are subtracted by separately subtracting their $x$, $y$, and $z$ components. If $\mathbf{v}_1=x_1 \hat{\mathbf{i}}+y_1 \hat{\mathbf{j}}+z_1 \hat{\mathbf{k}}$ and $\mathbf{v}_2=x_2 \hat{\mathbf{i}}+y_2 \hat{\mathbf{j}}+z_2 \hat{\mathbf{k}}$, then $\mathbf{v}_1-\mathbf{v}_2=\left(x_1-x_2\right) \hat{\mathbf{i}}+\left(y_1-y_2\right) \hat{\mathbf{j}}+\left(z_1-z_2\right) \hat{\mathbf{k}}$.
3. Multiplication by a scalar. A vector is multiplied by a scalar by separately multiplying each component by the scalar. If $\mathbf{v}=x \hat{\mathbf{i}}+y \hat{\mathbf{j}}+z \hat{\mathbf{k}}$, then $a \mathbf{v}=a x \hat{\mathbf{i}}+a y \hat{\mathbf{j}}+a z \hat{\mathbf{k}}$.
4. Division by a scalar. A vector is divided by a scalar by separately dividing each component by the scalar. If $\mathbf{v}=x \hat{\mathbf{i}}+y \hat{\mathbf{j}}+z \hat{\mathbf{k}}$, then $\frac{\mathbf{v}}{a}=\frac{x}{a} \hat{\mathbf{i}}+\frac{y}{a} \hat{\mathbf{j}}+\frac{z}{a} \hat{\mathbf{k}}$
5. The dot product. The dot product of two vectors is one form of multiplication operation performed on vectors. It produces a scalar that is the sum of the products of the vector's components. If $\mathbf{v}_1=x_1 \hat{\mathbf{i}}+y_1 \hat{\mathbf{j}}+z_1 \hat{\mathbf{k}}$
( GRAPH CAN'T COPY )
and $\mathbf{v}_2=x_2 \hat{\mathbf{i}}+y_2 \hat{\mathbf{j}}+z_2 \hat{\mathbf{k}}$, then the dot product of the vectors is $\mathbf{v}_1 \cdot \mathbf{v}_2=x_1 x_2+y_1 y_2+z_1 z_2$.
The cross product. The cross product is another multiplication operation that appears frequently between vectors. The cross product of two vectors is another vector whose direction is perpendicular to the plane formed by the two input vectors. If $\mathbf{v}_1=x_1 \hat{\mathbf{i}}+y_1 \hat{\mathbf{j}}+z_1 \hat{\mathbf{k}}$ and $\mathbf{v}_2=x_2 \hat{\mathbf{i}}+y_2 \hat{\mathbf{j}}+z_2 \hat{\mathbf{k}}$, then the cross product of the two vectors is defined as $\mathbf{v}_1 \times \mathbf{v}_2=\left(y_1 z_2-y_2 z_1\right) \hat{\mathbf{i}}+\left(z_1 x_2-z_2 x_1\right) \hat{\mathbf{j}}+\left(x_1 y_2-x_2 y_1\right) \hat{\mathbf{k}}$.
7. Magnitude. The magnitude of a vector is defined as $\mathbf{v}=\sqrt{x^2+y^2+z^2}$.
Create a class called Vector3D having three properties $x, y$, and $z$. Define constructors to create vector objects from three input values. Define get and put access methods for each property, and define methods to perform the seven vector operations defined in the preceding list. Be sure to design the methods so that they work with operator overloading when possible. Then create a program to test all of the functions of your new class.

Problem 6

The cross product. The cross product is another multiplication operation that appears frequently between vectors. The cross product of two vectors is another vector whose direction is perpendicular to the plane formed by the two input vectors. If $\mathbf{v}_1=x_1 \hat{\mathbf{i}}+y_1 \hat{\mathbf{j}}+z_1 \hat{\mathbf{k}}$ and $\mathbf{v}_2=x_2 \hat{\mathbf{i}}+y_2 \hat{\mathbf{j}}+z_2 \hat{\mathbf{k}}$, then the cross product of the two vectors is defined as $\mathbf{v}_1 \times \mathbf{v}_2=\left(y_1 z_2-y_2 z_1\right) \hat{\mathbf{i}}+\left(z_1 x_2-z_2 x_1\right) \hat{\mathbf{j}}+\left(x_1 y_2-x_2 y_1\right) \hat{\mathbf{k}}$.

Check back soon!

Problem 6

If no exceptions are thrown within a try block, where does execution continue after the try block is finished? If an exception is thrown within a try block and caught in a catch block, where does execution continue after the catch block is finished?

Check back soon!

Problem 7

Modify the FileWriter class by adding new methods to write numerical data to the file as text strings, with one numerical value per line.

Check back soon!

04:55
Problem 8

Example 6.4 shows how to create a simple random number generator with a uniform distribution over the range $[0,1)$. Exercise 6.33 shows how to create a random number generator with a Gaussian distribution, and Exercise 6.36 shows how to create a random number generator with a Rayleigh distribution.
Create a random number generation class that includes a way to set the initial seed in a constructor and through a set method. The random seed should be an instance variable of the class. The class should include methods to return random numbers drawn from uniform, Gaussian, and Rayleigh distributions. Test your program by creating arrays of uniform, Gaussian, and Rayleigh distributed values, and create histograms of the data in each array. Do the distributions have the right shape?

Chapter 12, User-Defined Classes and Object-Oriented Programming Video Solutions, MATLAB Programming for Engineers | Numerade (13)

Jessica Waggener

Numerade Educator

01:51
Problem 9

Demonstrate that each instance of the random number class in Exercise 12.8 is independent by performing the following steps: Create three objects from this class. Initialize two of them with the random seed 123456, and initialize the third one with the random number seed 654321 . Get the first 5 Gaussian distributed values from each object.

Chapter 12, User-Defined Classes and Object-Oriented Programming Video Solutions, MATLAB Programming for Engineers | Numerade (16)

Neel Faucher

Numerade Educator

01:28
Problem 10

Create a MATLAB class called dice to simulate the throw of a fair die by returning a random integer between 1 and 6 every time that the class is called. Design the class so that it keeps track of the total number of calls to each object. When the object is destroyed, it should print out the total number of times that it was used.

Chapter 12, User-Defined Classes and Object-Oriented Programming Video Solutions, MATLAB Programming for Engineers | Numerade (19)

Jerrah Biggerstaff

Numerade Educator

Chapter 12, User-Defined Classes and Object-Oriented Programming Video Solutions, MATLAB Programming for Engineers | Numerade (2024)

FAQs

Is MATLAB good for OOP? ›

Using object-oriented programming in MATLAB, you can manage software complexity by organizing your code into logical components that are easier to maintain and extend. You can avoid code duplication by creating reusable objects with well-defined interfaces that hide the complexity of the underlying code.

Is MATLAB object-oriented or procedural? ›

Object-oriented software encapsulates data and operations in objects that interact with each other via the object's interface. The MATLAB® language enables you to create programs using both procedural and object-oriented techniques and to use objects and ordinary functions together in your programs.

When did MATLAB add object-oriented programming? ›

Major enhancements to MATLAB object-oriented programming capabilities were made in 2008.

What programming language does MATLAB use? ›

MATLAB
L-shaped membrane logo
Developer(s)MathWorks
Written inC/C++, MATLAB
Operating systemWindows, macOS, and Linux
PlatformIA-32, x86-64, ARM64
7 more rows

Is MATLAB harder than Python? ›

The Difference in Technical Computing:

They are both used for the same type of work, such as numerical analysis, data visualization, and scientific computation. When it comes to syntax and readability, Python is often easier to read and understand than MATLAB.

Which is better MATLAB or C++? ›

Performance: C++ is known for its high performance and efficiency, as it allows direct memory access and low-level control over system resources. MATLAB, although optimized for numerical computations, may suffer from slower execution times for certain tasks due to its interpreted nature.

Is MATLAB used anymore? ›

As of May 2022, LinkedIn searches return about 7.6 million Python users and 4.1 million MATLAB users. People who do not work in engineering or science are often surprised to learn how widespread MATLAB is adopted, including: Millions of users in colleges and universities. Thousands of startups.

Is MATLAB based on C++? ›

In MATLAB classes, there is no equivalent to C++ templates or Java generics. However, MATLAB is weakly typed and it is possible to write functions and classes that work with different types of data. MATLAB classes do not support overloading functions using different signatures for the same function name.

Is Python an OOP? ›

The answer is quite simple: Yes! Python is an OOP language, but it is not purely OOP. To be precise, Python is a multi-paradigm language. Like Lisp and C++, it supports several different approaches.

Is MATLAB difficult to learn? ›

MATLAB® is not hard to learn if you go for any professional course. It is ideal for engineering graduates and IT professionals willing to develop MATLAB® skills in their related fields.

Is MATLAB real coding? ›

MATLAB is a high-level programming language designed for engineers and scientists that expresses matrix and array mathematics directly.

What is MATLAB useful for? ›

MATLAB® is a programming platform designed specifically for engineers and scientists to analyze and design systems and products that transform our world. The heart of MATLAB is the MATLAB language, a matrix-based language allowing the most natural expression of computational mathematics.

Does MATLAB support ++? ›

No, you cannot do this in Matlab. To increment a variable, you must use i = i + 1; .

Is MATLAB enough for a job? ›

Conclusion. The industry has some familiar buzz that learning MATLAB will not be a good opportunity for a better career. But this is not fully true. Yes, it is an acceptable reason that salary or company structure will never be able to touch available popular jobs on other programming technologies.

Can MATLAB detect cheating? ›

MATLAB Grader does not contain a built-in solution for actively monitoring submissions and preventing cheating. However, for courses run on the MATLAB Grader platform, instructors do have access to all submissions made for a problem. Student submissions can be downloaded and analyzed in the instructor's preferred tool.

What is MATLAB most useful for? ›

Control engineers can employ MATLAB in every development stage. They may use it for modeling, controller design, or to deploy control algorithms to embedded systems that require real-time tuning. Data architects, engineers, and scientists use MATLAB for database management, data processing, and data cleaning.

References

Top Articles
Latest Posts
Article information

Author: Patricia Veum II

Last Updated:

Views: 5396

Rating: 4.3 / 5 (44 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Patricia Veum II

Birthday: 1994-12-16

Address: 2064 Little Summit, Goldieton, MS 97651-0862

Phone: +6873952696715

Job: Principal Officer

Hobby: Rafting, Cabaret, Candle making, Jigsaw puzzles, Inline skating, Magic, Graffiti

Introduction: My name is Patricia Veum II, I am a vast, combative, smiling, famous, inexpensive, zealous, sparkling person who loves writing and wants to share my knowledge and understanding with you.