p=[]; %creates an empty polynomial degree=input(\'Please enter the degree of the
ID: 3560600 • Letter: P
Question
p=[]; %creates an empty polynomial
degree=input('Please enter the degree of the polynomial: ');
j=degree; %this is used for correct message display below
degree=degree+1;
for i=1:1:degree
fprintf('Please enter the coefficient of power %d ',j);
p(i)=input(' ');
j=j-1;
end
%note the highest degree coefficient is stored in position 1 and the next one is position 2 and so on
%evaluating the polynomial at a point
%plotting
lowerLimit=input('Please enter a value for the lower limit > ');
upperLimit=input('Please enter a value for the upper limit > ');
resolution=input('Please enter a value for the resolution, e.g. 0.01 > ');
%x-y plot requires the polynomial to be evaluated at each x point
%therefore we need to use polyval
x=[lowerLimit:resolution:upperLimit];
y=polyval(p,x);
plot(x,y)
P1: Add another set of codes that will calculate and display the roots of the polynomial.
P2: Add another set of codes that will plot another polynomial and plots both polynomials. User will enter relevant plot information
P3. Write a Matlab script that will ask the user to enter a function and plot it using the fplot command. User will enter relevant plot information.
* Relevant plot information consists of plot title, x-axis title, y-axis title, x-axis range, and y-axis range.
P4. Redo problem 4.19 (from week 3) using Matlab.
4.19 : When sound travels through a gas, its speed depends primarily on the density of the medium. The less dense the medium, the faster the speed will be. The following table shows the approximate speed of sound at 0 degrees centigrade, measured in meters per second, when traveling through carbon dioxide, air, helium, and hydrogen.
medium speed
Carbon Dioxide 258.0
Air 331.5
Helium 972.0
Hydrogen 1,270.0
Write a program that displays a menu allowing the user to select one of these four gases. After a selection has been made, the user should enter the number of seconds it took for the sound to travel in this medium from its source to the location at which it was detected. The program should then report how far awat in meters the sound source was from the detection location.
Input Validation: Check that the user has selected one of the available choices from the menu. Do not accept times less than 0 sec or more than 30 sec .
P5.
5.16 : Write a MATLAB program that calculates the balance of a savings account at the end of a period of time. It should ask the user for the annual interest rate, the starting balance, and
the number of months that have passed since the account was established. A loop
should then iterate once for every month, performing the following:
A)sk the user for the amount deposited into the account during the month. (Do not accept negative numbers.) This amount should be added to the balance.
B) ask the user for the amount withdrawn from the account during the month. (Do not accept negative numbers.) This amount should be subtracted from the balance.
C) calculate the monthly interest. The monthly interest rate is the annual interest rate divided by twelve. Multiply the monthly interest rate by the balance, and add the result to the balance.
After the last iteration, the program should display the ending balance, the total amount of deposits, the total amount of withdrawals, and the total interest earned.
NOTE: If a negative balance is calculated at any point, a message should be displayed indicating the account has been closed and the loop should terminate.
P6. (similar to P3 from week 4). Write a Matlab script that will ask the user for the number of exam grades to be processed. Next, the program will ask for the actual grades, using a for loop, calculates the average and displays the average and finally it will display the letter grade.
Grades are assumed to be between 0 and100; grades outside the range would not be counted toward the calculations.
Note: Data validation is required; there is no
Explanation / Answer
MATLAB represents polynomials as row vectors containing coefficients ordered by descending powers. For example, the equation P(x) = x4 + 7x3 - 5x + 9 could be represented as:
p = [1 7 0 -5 9];
Evaluating Polynomials
The polyval function is used for evaluating a polynomial at a specified value. For example, to evaluate our previous polynomial p, at x = 4, type:
MATLAB executes the above statements and returns the following result:
MATLAB also provides the polyvalm function for evaluating a matrix polynomial. A matrix polynomial is apolynomial with matrices as variables.
For example, let us create a square matrix X and evaluate the polynomial p, at X:
MATLAB executes the above statements and returns the following result:
Finding the Roots of Polynomials
The roots function calculates the roots of a polynomial. For example, to calculate the roots of our polynomial p, type:
MATLAB executes the above statements and returns the following result:
The function poly is an inverse of the roots function and returns to the polynomial coefficients. For example:
MATLAB executes the above statements and returns the following result:
Polynomial Curve Fitting
The polyfit function finds the coefficients of a polynomial that fits a set of data in a least-squares sense. If x and y are two vectors containing the x and y data to be fitted to a n-degree polynomial, then we get the polynomial fitting the data by writing:
Example
Create a script file and type the following code:
When you run the file, MATLAB displays the following result:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.