We have developed a MATLAB code for the secant method satisfying the requirements.
MATLAB code
f=@(x) x^4-x^3-7*x^2+x+6;
[x1,x2,epsilon]=get_input();
[x,curr_error,it]=secant_method(f,x1,x2,epsilon);
plot_result(x,curr_error,it)
function [x1,x2,epsilon]=get_input()
fprintf('\n\n');
prompt1='Enter an initial guess x1: '; x1= input(prompt1);
prompt2='Enter an initial guess x2: '; x2= input(prompt2);
prompt3='enter a value for epsilon: '; epsilon=
input(prompt3);
while epsilon<=0
disp(['Invalid epsilon ',num2str(epsilon)]);
prompt3='enter a value for epsilon: ';
epsilon= input(prompt3);
end
end
function [x,curr_error,it]=secant_method(f,x1,x2,epsilon)
x(1)=x1;
x(2)=x2;
k=2;
curr_error(1)=3;
while curr_error(k-1)>epsilon
k=k+1;
xn1=x(k-2);
xn2=x(k-1);
x(k)=xn1-f(xn1)*(xn1-xn2)/(f(xn1)-f(xn2));
curr_error(k-1)=abs(x(k)-x(k-1));
end
fprintf('\nAn approximation of the root is x(%g)=%f',k,x(k));
fprintf('\nAn approximation of the absolute error is
%e\n\n',curr_error(end));
it=1:k;
end
function plot_result(x,curr_error,it)
figure(1);
plot(it,x,'LineWidth',1.5);
xlabel('Iteration Number','fontsize',14)
ylabel('Root Approximation','fontsize',14)
title('Iteration Number vs Root Approximation','fontsize',14)
print('-dpng','secant_method_approx.png');
figure(2);
semilogy(it(2:end),curr_error,'LineWidth',1.5);
xlabel('Iteration Number','fontsize',14)
ylabel('Error Approximation','fontsize',14)
title('Iteration Number vs Error
Approximation','fontsize',14)
print('-dpng','secant_method_error.png');
end
Please code in MatLab or Octave Output should match Sample Output in Second Picture Thank you...
Please code in MatLab or Octave Output should match Sample Output in the second picture Thank You 4. In this problem we will investigate using loops to calculate the product of two matrices. Given an mxn matrix A and an n x p matrix B, the matrix product C = AB is the m xp matrix with Cij = R=1 Qikbky. Write a program which calls a function get matrix.dimensions which should • print a description of the purpose of...
This program has to be written in matlab 2. Write a function to find a root of f(c) using the secant method. Call this routine secant, call the file secant.m; its first line should be function [x,nf] = secant (fname, x0, x1, tol) fname is the name of the m-file which evaluates the function f(x), 20 and 21 are initial approximations to .c", and tol is a stopping tolerance. Your code should return an approximation x = 2X+1 to 3*...
Write a matlab program to implement the secant root finding method in matlab. The function name should be Secant and it should take the equation as input whoes root has to be found and the two initial values of a and b and maximum tolerable error. Consider the following example: Your code should generate the following: >> secantAssg5(@(x)(x^4+x^2+x+10),2,3,0.0001) Xn-1 f(Xn-1) Xn f(Xn) Xn+1 f(Xn+1) 2.0000 32.0000 3.0000 103.0000 1.5493 19.7111 ….. ….. ….. Root is x = 0.13952 ans...
B. Implement the Newton-Raphson (NR) method for solving nonlinear equations in one dimension. The program should be started from a script M-file. Prompt the user to enter an initial guess for the root. -Use an error tolerance of 107, -Allow at most 1000 iterations. .The code should be fully commented and clear 2. a) Use your NR code to find the positive root of the equation given below using the following points as initial guesses: xo = 4, 0 and-1...
A. Implement the False-Position (FP) method for solving nonlinear equations in one dimension. The program should be started from a script M-file. -Prompt the user to enter lower and upper guesses for the root. .Use an error tolerance of 107. Allow at most 1000 iterations The code should be fully commented and clear " 1. Use your FP and NR codes and appropriate initial guesses to find the root of the following equation between 0 and 5. Plot the root...
Task 2 (25 points + 4 points for commenting): Write computer code to perform the Fixed.Point method and use your code to find the root of the following equation using an initial guess of 3 and a stopping criterion of 0.001%; f(x) e -4x For Fixed-Point, you do not have to code Matlab to take the derivatives of the function and check the g'(x). You can do that step by hand, and then show me your hand cal ulations to...
Newton's Method in MATLAB During this module, we are going to use Newton's method to compute the root(s) of the function f(x) = x° + 3x² – 2x – 4 Since we need an initial approximation ('guess') of each root to use in Newton's method, let's plot the function f(x) to see many roots there are, and approximately where they lie. Exercise 1 Use MATLAB to create a plot of the function f(x) that clearly shows the locations of its...
Problem 4 (programming): Create a MATLAB function named mynewton.m to estimate the root for any arbitrary function f given an initial guess xo, an absolute error tolerance e and a maximum number of iterations max iter. Follow mynewton.m template posted in homework 2 folder on TritonED for guidance. You are not required to use the template. The function should return the approximated root ^n and the number of steps n taken to reach the solution. Use function mynewton.m to perform...
Please do not use SYMS package. It does not work on Octave for me. Matlab code needed for: 1. Apply the Explicit Trapezoid Method on a grid of step size h = 0.1 in [0, 1] to the initial value problems in Exercise 1. Print a table of the t values, approximations, and global truncation error at each step. IVP (Exercise 1): (a) y'=1 (b) y' = 12y (c) y' = 2(t+1)y (d) y = 564, (e) y'=1/y² (1) y'=1/y2...
in C++. Write a function squareRoot that uses the Newton’s method of approximate calcu-lation of the square root of a number x. The Newton’s method guesses the square root in iterations. The first guess is x/2. In each iteration the guess is improved using ((guess + x/guess) / 2 ) as the next guess. Your main program should prompt the user for the value to find the square root of (x) and how close the final guess should be to...