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 Vaia!

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 a MATLAB program to evaluate the function $$ y(x)=\ln \frac{1}{1-x} $$ for any user-specified value of \(x\), where In is the natural logarithm (logarithm to the base e). Write the program with a while loop, so that the program repeats the calculation for each legal value of \(x\) entered into the program. When an illegal value of \(x\) is entered, terminate the program. (Any \(x \geq 1\) is considered an illegal value.)

Harmonic Mean. The harmonic mean is yet another way of calculating a mean for a set of numbers. The harmonic mean of a set of numbers is given by the equation $$ \text { harmonic mean }=\frac{N}{\frac{1}{x_{1}}+\frac{1}{x_{2}}+\ldots+\frac{1}{x_{N}}} $$ Write a MATLAB program that will read in an arbitrary number of positive input values and calculate the harmonic mean of the numbers. Use any method that you desire to read in the input values. Test your program by calculating the harmonic mean of the four numbers \(10,5,2\), and \(5 .\)

Declbels. Engineers often measure the ratio of two power measurements in decibels, or dB. The equation for the ratio of two power measurements in decibels is $$ d B=10 \log _{10} \frac{P_{2}}{P_{1}} $$ where \(P_{2}\) is the power level being measured, and \(P_{1}\) is some reference power level. Assume that the reference power level \(P_{1}\) is 1 watt, and write a program that calculates the decibel level corresponding to power levels between 1 and 20 watts, in \(0.5 \mathrm{~W}\) steps. Plot the dB-versus- power curve on a log-linear scale.

Tension on a Cable. A 200-pound object is to be hung from the cnd of a rigid 8 -foot horizontal pole of negligible weight, as shown in Figure 4.5. The pole is attached to a wall by a pivor and is supported by an 8 -foot cable that is attached to the wall at a higher point. The tension os this cable is given by the equation $$ T=\frac{W+k c \cdot l p}{d \sqrt{l p^{2}-d^{2}}} $$ where \(T\) is the tension on the cable, \(W\) is the weight of the object. Ic is the length of the cable, Ip is the length of the pole, and \(d\) is the distance along the pole at which the cable is attached. Write a program to determine the distance \(d\) at which to attach the cable to the pole to minimize the tension on the cable. To do this, the program should calculate the tension on the cable at regular l-foot intervals from \(d=1\) foot to \(d=7\) fect. and should locate the position \(d\) that produces the minimum tens on. Also, the program should plot the tension on the cable as a function of \(d\), with appropriate tities and axis labels.

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.

Sign-up for free