/*! This file is auto-generated */ .wp-block-button__link{color:#fff;background-color:#32373c;border-radius:9999px;box-shadow:none;text-decoration:none;padding:calc(.667em + 2px) calc(1.333em + 2px);font-size:1.125em}.wp-block-file__button{background:#32373c;color:#fff;text-decoration:none} Problem 4 Write an M-file to evaluate the ... [FREE SOLUTION] | 91Ó°ÊÓ

91Ó°ÊÓ

Write an M-file to evaluate the equation \(y(x)=x^{2}-3 x+2\) for all values of \(x\) between \(0.1\) and 3 , in steps of \(0.1\). Do this twice, once with a for loop and once with vectors. Plot the resulting function using a 3 point thick dashed red line.

Short Answer

Expert verified
Create an M-file and define 'x' values using linspace. Evaluate the equation \(y(x) = x^2 - 3x + 2\) for each 'x' value using a for loop and store results in 'y_for'. Next, evaluate the equation using vectors and store results in 'y_vec'. Plot the function with a 3 point thick dashed red line, label axes and title, and enable the grid. Finally, save and run the script to display the resulting plot.

Step by step solution

01

Define x values using linspace

In the script, define the x values between 0.1 and 3 with an increment of 0.1: ``` x = linspace(0.1, 3, (3-0.1)/0.1); ``` Step 3: Evaluation using for loop
02

Evaluate equation using for loop

Use a for loop to evaluate the equation for each x value and store the result in a new array 'y_for': ``` y_for = zeros(size(x)); for i = 1:length(x) y_for(i) = x(i)^2 - 3*x(i) + 2; end ``` Step 4: Evaluation using vectors
03

Evaluate equation using vectors

Now, evaluate the equation using vectors and store the result in a new array 'y_vec': ``` y_vec = x.^2 - 3.*x + 2; ``` Step 5: Plotting the function
04

Plot the function with a dashed red line

Plot the function with a 3 point thick dashed red line. Since the results from the for loop and vectors are the same, we can use any of the y arrays for plotting: ``` plot(x, y_for, 'r--', 'LineWidth', 3); xlabel('x'); ylabel('y(x)'); title('y(x) = x^2 - 3x + 2'); grid on; ``` Step 6: Save and run the script Save the 'FunctionEvaluation.m' script and then run it in the MATLAB environment. The resulting plot will display the evaluated function as a dashed red line.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with 91Ó°ÊÓ!

Key Concepts

These are the key concepts you need to understand to accurately answer the question.

For Loop in MATLAB
A "for loop" in MATLAB is a basic programming construct that allows you to repeat a group of commands a specific number of times. This is particularly useful when you need to perform repetitive calculations, like evaluating a function at multiple points. For loops in MATLAB are defined by specifying the range of iterations and the block of code to be executed during each iteration.

To demonstrate, consider the equation we'll evaluate: \(y(x) = x^2 - 3x + 2\). Using a for loop, we can iterate over our defined range of x values from 0.1 to 3, with steps of 0.1. Here’s a closer look at the key points:
  • Initialize the result array, `y_for`, to store output values.
  • Write a loop that goes from \(1\) to the length of the `x` array.
  • During each iteration, calculate \(y\) based on the equation and store it in `y_for`.

The for loop will evaluate \(x(i)^2 - 3*x(i) + 2\) for each \(x(i)\), thus populating `y_for` with corresponding \(y\) values.
Vectorization in MATLAB
Vectorization in MATLAB makes your code run faster and more efficiently by replacing explicit loops with vector operations. MATLAB is optimized for operations on arrays and matrices, so taking advantage of vectorized code often leads to better performance. Instead of iterating through each element manually, vectorization processes all elements at once using vectorized calculations.

For the same equation, \(y(x) = x^2 - 3x + 2\), vectorization allows for a cleaner and more concise code approach. Here's what vectorization looks like:
  • Define `x` using `linspace` to create your vector of values.
  • Apply the formula directly to the vector `x`, without using loops: `y_vec = x.^2 - 3.*x + 2`.

The above statement will perform the calculations on every element of `x` simultaneously. This not only simplifies the code but also capitalizes on MATLAB's strengths in handling array operations.
Plotting in MATLAB
Plotting in MATLAB is a straightforward way to visualize data, allowing you to see trends and patterns quickly. In this exercise, you need to plot the evaluated function \(y(x)\) against `x` values from 0.1 to 3. MATLAB offers various options for customizing plots, making it a versatile tool for data visualization.

For our function, the plot is specified with a red dashed line that is 3 points thick. Here is how to achieve this using the `plot` function:
  • Use the command `plot(x, y_for, 'r--', 'LineWidth', 3);` where `'r--'` denotes a red dashed line.
  • Enhance the plot with labels using `xlabel('x')` and `ylabel('y(x)')` to clarify the axes.
  • Add a title with `title('y(x) = x^2 - 3x + 2')`.
  • Enable the grid using `grid on` for better readability of the plot.

Once you've set up the code, executing it will display the function on a graph, allowing for an easy and effective analysis of the evaluated mathematical expression.

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

Modify program 1sqfit from Example \(4.7\) to read its input values from an ASCII file named input.1. dat. The data in the file will be organized in rows, with one pair of \((x, y)\) values on each row, as shown below: $$ \begin{array}{ll} 1.1 & 2.2 \\ 2.2 & 3.3 \end{array} $$ Test your program using the same two data sets that were used in Example 4.7. (Hint: Use the load command to read the data into an array named input1, and then store the first column of inpat 1 into array \(x\) and the second column of input 1 into array \(y\).)

Write the MATLAB statements required to calculate and print out the squares of all the even integers between 0 and 50 . Create a table consisting of each integer and its square, with appropriate labels over each column.

Current Through a Diode. The current flowing through the semiconductor diode shown in Figure \(4.4\) is given by the equation $$ i_{\theta}=l_{e}\left(e^{\frac{w_{3}}{1 T}}-1\right) $$ where $$ \begin{aligned} &i_{D}=\text { the voltage across the diode, in volts } \\ &v_{n}=\text { the current flow through the diode, in amps } \\ &I_{e}=\text { the leakage current of the diode, in amps } \\ &q=\text { the charge on an electron, } 1.602 \times 10^{-19} \text { coulombs } \\ &k=\text { Boltzmann's constant, } 1.38 \times 10^{-23} \text { joule/K } \\ &T=\text { temperature, in kelvins }(\mathrm{K}) \end{aligned} $$ The leakage current \(I_{o}\) of the diode is \(2.0 \mu \mathrm{A}\). Write a program to calculate the current flowing through this diode for all voltages from \(-1.0 \mathrm{~V}\) to \(+0.6 \mathrm{~V}\), in \(0.1 \mathrm{~V}\) steps. Repeat this process for the following temperatures: \(75^{\circ} \mathrm{F}, 100^{\circ} \mathrm{F}\), and \(125^{\circ} \mathrm{F}\), Create a plot of the current as a function of applied voltage, with the curves for the three different temperatures appearing as different colors.

Bacterial Growth. Suppose that a biologist performs an experiment in which he or she measures the rate at which a specific type of bacterium reproduces asexually in different culture media. The experiment shows that in Medium A the bacteria reproduce once every 60 minutes, and in Medium B the bacteria reproduce once every 90 minutes. Assume that a single bacterium is placed on each culture medium at the beginning of the experiment. Write a program that calculates and plots the number of bacteria present in cach culture at intervals of 3 hours from the beginning of the experiment antil 24 hours have elapsed. Make two plots, one a linear xy plot and the other a linear-log (semi logy) plot. How do the numbers of bacteria compare on the two media after 24 hours?

Mean Time Between Failure Calculations. The reliability of a piece of electronic equipment is usually measured in terms of mean time between failures (MTBF), where MTBF is the average time that the piece of equipment can operate before a failure occurs in it. For large systems containing many pieces of electronic equipment, it is customary to determine the MTBFs of each component and to calculate the overall MTBF of the system from the failure rates of the individual components. If the system is structured like the one shown in Figure 4.6, every component must work in order for the whole system to work, and the overall system MTBF can be calculated as $$ \mathrm{MTBF}_{\mathrm{yy}}=\frac{1}{\frac{1}{\mathrm{MTBF}_{1}}+\frac{1}{\mathrm{MTBF}_{2}}+\ldots+\frac{1}{\mathrm{MTBF}_{n}}} $$ Write a program that reads in the number of series components in a system and the MTBFs for each component, and then calculates the overall MTBF for the system. To test your program, determine the MTBF for a radar system consisting of an antenna subsystem with an MTBF of 2000 hours, a transmitter with an MTBF of 800 hours, a receiver with an MTBF of 3000 hours, and a computer with an MTBF of 5000 hours.

See all solutions

Recommended explanations on Psychology Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.