正在加载图片...
2.Turn pseudo-code into real code Write a MATLAB code to find the zero of a function.Your code will be made up of three M-files: a script M-file to drive the process a function M-file that finds zeros using a Newton procedure (explained below) a function M-file that contains the function for which you are finding the zero. Pseudo-code for the three pieces is given below.Construct the MATLAB code. Script M-file (a)Prompt the user for an initial guess for the x value at which the zero occurs. (b)Call the zero-finding function,with the initial guess as the input argument,and the final answer and number of steps it took to find the final answer as the output arguments. (c)Print out the final answer and the number of steps it took to find the final answer in some self-explanatory format. Function M-file for finding a Zero (a)This function has one input argument,the initial guess for x. (b)This function has two output arguments,the final approximation of the xvalue at which the zero is found,and the number of steps it took to find it. (c)Set a convergence criterion of s=10-6. (d)Initialize the number of iterations to zero. (e)Carry out the following steps until the absolute value of f(x)<&where f is the function you are zeroing,and x is your current approximation of the zero. Calculate an approximation to the derivative of the function at this point,from f(c)=f(r+e)-f(c-) 2e Calculate a new approximation to the zero,from x=x-f(x)/f(x) Update the number of steps that you have taken through the loop. (⑤Return to main Hint:Function Prototype function [x n]findZero (initGuess) Function M-file f (a)This function has one input argument,x. (b)This function has one output argument,y. (c)The relation is y=x2-sin()/x; Hint:Function Prototype function y=yValue (x)2. Turn pseudo-code into real code Write a MATLAB code to find the zero of a function. Your code will be made up of three M-files: • a script M-file to drive the process • a function M-file that finds zeros using a Newton procedure (explained below) • a function M-file that contains the function for which you are finding the zero. Pseudo-code for the three pieces is given below. Construct the MATLAB code. Script M-file (a) Prompt the user for an initial guess for the x value at which the zero occurs. (b) Call the zero-finding function, with the initial guess as the input argument, and the final answer and number of steps it took to find the final answer as the output arguments. (c) Print out the final answer and the number of steps it took to find the final answer in some self-explanatory format. Function M-file for finding a Zero (a) This function has one input argument, the initial guess for x. (b) This function has two output arguments, the final approximation of the xvalue at which the zero is found, and the number of steps it took to find it. (c) Set a convergence criterion of ε = 10−6 . (d) Initialize the number of iterations to zero. (e) Carry out the following steps until the absolute value of f(x) < ε where f is the function you are zeroing, and x is your current approximation of the zero. • Calculate an approximation to the derivative of the function at this point, from • Calculate a new approximation to the zero, from • Update the number of steps that you have taken through the loop. (f) Return to main Hint: Function Prototype function [x n] = findZero (initGuess) Function M-file f (a) This function has one input argument, x. (b) This function has one output argument, y. (c) The relation is Hint: Function Prototype function y = yValue (x)
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有