Question

In: Mechanical Engineering

Practice for Matlab. You can create own f(x). a. Write code to find and plot a...

Practice for Matlab. You can create own f(x).

a. Write code to find and plot a root using the modified secant method

b. Find the roots using the roots command - identify and plot root of interest

c. Use polyder and roots to find and plot the minimum value

d. use fminbnd to find and plot minimum value

Thank you!

Solutions

Expert Solution

The matlab code is given below:

Matlab code:

close all; clear all; clc
F = @(x) x^2-10.56*x+5.6; % Define the function
p = [1 -10.56 5.6]; % Define same polynomial function with coefficients

[x,e] = Modified_Secant_method(F,0.1,0,10,0.1) % Used Modified Secant method
figure % New figure
title('Using roots')
scatter(real(x),imag(x),'filled','red') % Plot solution from modified secant method

r = roots(p); % Use roots method
figure % New figure
title('Using roots')
xlabel('x value')
ylabel('Minimum value')
scatter(real(r),imag(r),'filled','red') % Plot solution from roots

dF = polyder(p); % Polyder to find differential of equation
rd = roots(dF); % Find the roots of differentiated polynomial
ddF = polyder(dF);
for i = 1:length(rd)
if(polyval(ddF,rd(1)) > 0)
minr = rd;
end
end
figure % New figure
title('Using polyder')
plot(minr,F(minr),'*r')% Plot minimum value
xlabel('x value')
ylabel('Minimum value')

figure
title('Using fminbnd')
xmin = fminbnd(F,0,10); % Find value of x for which the function is minimum
plot(xmin,F(xmin),'*r')% Plot minimum value
xlabel('x value')
ylabel('Minimum value')

The script that calculates roots using modified secant method is given below:

Modified_Secant_method.m:

function [x,e] = Modified_Secant_method(F,del,x_0,N,err)

x(1) = x_0;
i = 1;
e = 10;
while((i<N) & (e>err))
x(i+1) = x(i)-(F(x(i))*del)/(F(x(i)+del)-F(x(i)));
if (i ~= 1)
e(i) = (abs(x(i+1)-x(i))/x(i+1))*100;
end
i = i+1;
end
end


Related Solutions

This code is to be written in Matlab. Write a function that will plot cos(x) for...
This code is to be written in Matlab. Write a function that will plot cos(x) for x values ranging from -pi to pi in steps of 0.1, using black *'s. It will do this three times across in one Figure Window, with varying line widths. If no arguments are passed to the function, the line widths will be 1, 2, and 3. If on the other hand, an argument is passed to the function, it is multiplier for these values....
Write your own MATLAB code to solve the system 10 − x + sin(x + y)...
Write your own MATLAB code to solve the system 10 − x + sin(x + y) − 1 = 0 8y − cos2 (z − y) − 1 = 0 12z + sin(z) − 1 = 0 using a residual tolerance of 10^−6 and the initial guess, ~x 0 = [0.1, 0.25, 0.08]^T . Print out the values for x, y and z for each iteration in a table similar to the one you created for the problems of the...
Use matlab to plot Taylor approximation for f(x) for x near 0 Given f(x) = exp(x)...
Use matlab to plot Taylor approximation for f(x) for x near 0 Given f(x) = exp(x) Use subpots to plot and its absolute and realative erros for N= 1,2,3 PLease give matlab code for Taylor and explain in detail, Thank you
Given the plot of y=f(x) below, find the plot of y=f−1(x). A coordinate plane has a...
Given the plot of y=f(x) below, find the plot of y=f−1(x). A coordinate plane has a horizontal x-axis labeled from negative 7 to 7 in increments of 1 and a vertical y-axis labeled from negative 7 to 7 in increments of 1. A curve starts at the point left-parenthesis negative 1 comma 0 right-parenthesis, rises at an increasing rate from left to right and passes through left-parenthesis 1 comma 1 right-parenthesis and left-parenthesis 4 comma 6 right-parenthesis. Select the correct...
Matlab Consider f(x) = x^3 - x. Plot it. (a) Use Newton's method with p_0 =1.5...
Matlab Consider f(x) = x^3 - x. Plot it. (a) Use Newton's method with p_0 =1.5 to find an approximation to a root with tolerance TOL = 0.01. (b) Use secant method with p_0 = 0.5 , p_1 =1.5, to answer same question as (a). (c) Use Method of False position with initial approximate roots as in (b).
Please show MATLAB code to plot below function from x = −3 to x = 12:...
Please show MATLAB code to plot below function from x = −3 to x = 12: function f = piecewise(x) % implements piecewise function using if statements if x < 0     f = -x^3 - 2*x^2 + 3*x; elseif x <= 8     f = (12/pi) * sin(pi*x/4); else     f = (600*exp(x-8))/(7*(14 + 6*exp(x-8))) -30/7; end
MATLAB Question Write a script to plot the function 'x times sine-squared of x' over a...
MATLAB Question Write a script to plot the function 'x times sine-squared of x' over a user-specified interval. It should prompt the user to enter the starting and stopping values of x, perform the calculation, and plot the result. Test the script using start/stop values 28 and 42.
Write the MATLAB code to Create a new figure. Define a theta array such that ?...
Write the MATLAB code to Create a new figure. Define a theta array such that ? 2 ≤ ? ≤ 9? 2 with increment of ? 10 ; Create a sixmember array of ones called r. Create a new polar plot of ? versus r
(Optics) Write a program in Matlab that can plot the amplitude and energy reflection and transmission...
(Optics) Write a program in Matlab that can plot the amplitude and energy reflection and transmission coefficients for both polarizations as functions of incidence angle for any incidence dielectric medium into any other dielectric medium.
Matlab code problems I have to write a code using functions to find out if a...
Matlab code problems I have to write a code using functions to find out if a year is a leap year. I'm on the write track i feel like but I keep getting an error message and matlab isnt helping to troubleshoot. The issue is on line 30. Here is my code: function project_7_mfp() %   PROJECT_7_ABC   project_7_abc() creates a function that prompts the user %   enter a year after 1582 It then determines if it is a leap year %...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT