Question: I need help what am I doing wrong in my matlab code that isn't aligned with the instructions? first 4 photos are the instructions and

I need help what am I doing wrong in my matlab code that isn't aligned with the instructions? first 4 photos are the instructions and the ones after are my matlab code.

I need help what am I doing wrong in my matlabI need help what am I doing wrong in my matlabI need help what am I doing wrong in my matlabI need help what am I doing wrong in my matlabI need help what am I doing wrong in my matlabI need help what am I doing wrong in my matlabI need help what am I doing wrong in my matlabI need help what am I doing wrong in my matlabI need help what am I doing wrong in my matlabI need help what am I doing wrong in my matlabI need help what am I doing wrong in my matlabI need help what am I doing wrong in my matlab
Instructions: You will write a program in MATLAB to solve non-linear equations using the Newton-Raphson method and the Secant method. The application is from fluid mechanics. The friction factor, f, is a value that is sought to determine the pressure drop in pipes and channels. According to extensive experiments, Colebrook arrived at the following expression for the friction factor, f, which is referred to as the Colebrook equation in his honor 2 2.51 F_factor (f) :=f 2 + 2.0-log i + 1 Where, Ref i is the unknown friction factor is the ratio of surface roughness of the pipe wall to diameter of the pipe. It is a given value for a type of pipe material. Re is called the Reynolds number and it is a non-dimensional number characterizing the ratio of linear momentum to viscous effects in the pipe flow. It is a known value for a given fluid, pipe diameter and flow rate. = f famous Moody Chart which is difficult for most to read accurately, while we c readily compute the friction factor by using methods from EML 3034C. Once t friction factor is known, the pressure drop due to friction for a fluid of density p given by Ap = p(0) - p(L) = pgh, Where is the acceleration due to gravity (in appropriate units). is the density of the fluid. is the head loss due to friction for a pipe of length L and diameter. D in which fluid flows at a mean velocity V. The head loss is related to the friction factor by the relation: h, = f LV D 29 So that if the English system is used, the head loss, h, , is given in feet (of water) a it is the equivalent pressure due to a column of water of height h, . Assuming flow in pipe such that: L= 12500 ft length of pipe. D = 36 in diameter of pipe. r = 2.667x 10-4 pipe wall roughness ratio. V= 0.55 ft/s mean velocity in the pipe. Re =1.5x105 Reynolds number of the flow. g = 32.2 ft/s2 acceleration due to gravity.(1) Use the Newton Raphson method to solve the Colebrook equation for the friction factor in the pipe using an initial guess of f= 0.01 and f= 0.001 . You can determine the derivative of the Colburn equation by using the symbolic manipulator in Mathcad (on UCF apps) or in Mathematica (free use of certain functions online), or carry this out by hand (see appendix). (2) From the result computed in (1), find the head loss h, in feet. This code should output: the iteration number, the guess for the root at the current iteration, the iterative convergence at each iteration, and the residual at each iteration in one matrix, so that you can view the results in a single table._Set the stopping criteria to when both the residual and iterative convergence are less than 10-5. Note that it was necessary to compute the derivative of the Colburn relation to apply the Newton-Raphson method. An alternative for this problem would be the Secant method that does not require derivative evaluation. (3) Repeat the exercise using the Secant method. You will need to write a new routine to call to solve your problem. Use initial guesses of f=0.0/ and f=0.00/. (4) Report the friction factor and head loss as before which you now have obtained using the Secant method. How do the answers compare? It would be a good practice to write a main code that has the logic of the problem solution and two routines, one for Newton Raphson and one for Secant method, that can be called from the main routine to solve the equation of interest. Once your program(s) is(are) completed, you should add a plot of the residual vs. iteration number using the given initial guesses(s) for both Newton-Raphson and Secant method. Refer to last week's assignment if you need help creating a plot. This completes Project 2 assignment. Now report the required values on the Webcourses on-line quiz for project 2, specifically report: a) The friction factor that you found using the initial guess f=0.01 and fr0.001. b) The head loss in feet that you compute for the length of pipe. for Newton Raphson and for the Secant method (with the initial guesses of f=0.0/ and f=0.001). 10 Re = 1.5 * 10^5; L = 12500; 12 D = 36/12; 13 g = 32.2; V = 0.55; r = 2.667 * 10^-4; F = @(x) x.^(-0.5) + 2.*log10(r/3.7 + 2.51./ (Re. *x. ^(0.5) ) ); % for logarithm its log10( ) e is exp(1) exp() 19 df = @(x) -0.5.*x. ^(-1.5) + (((2.51/Re)*log10(exp(1) ) ) ./ ((r/3.7) + (2.51. / (Re. *x. ~(0.5) ) ) ) ) . *x. ^(-3/2) ; 20 h=@(f) f*L*V^2/ (D*2*g) ; 21 22 max_iter= 100; 23 guess1 = .01; 24 guess2 = .001; 25 res_tol = 10^-5; 26 conv_tol = res_tol; 27 28 % plot the function 29 X = 0. 001 :0.001:0.1;30 fval = f(x) ; 31 figure 32 plot (x, fval, x, zeros (length(x) ) ) 33 title(' Function to find the root' ) 34 35 % Run the solvers 36 fprintf('Newton Raphson \ ' ) 37 % call the newton_raphson ( ) function here 38 [roots_nr, conv_nr, res_nr, num_iters_nr] = . . 39 newton_raphson_func (f, df, guess1, conv_tol, res_tol, max_iter); 40 % [%outputs] = newton_raphson_func (%inputs) ) 41 % x nr, or x_sec 42 fprintf( ' \ \ = === (n' ) 43 fprintf( ' Secant\ ' ) 44 %% call the secant ( ) function here 45 [roots_s, conv_s, res_s, num_iters_s] = .. 46 secant ( f, guess2, conv_tol, res_tol, max_iter); 47 48 fprintf ( ' \ === (n ') 49 fprintf( ' Answers : \ ' )50 fprintf(' newton = %.6e in Xi iterations. \ ', roots_nr(end), num_iters_nr); 51 fprintf(' secant = %.6e in %i iterations. \ ', roots_s (end) , num_iters_s) ; 52 53 asnwer for NR and SEC are arrays, you have to print the last value 54 55 % plot the residual and convergences 56 plot_iters_nr = 0: length(res_nr)-1; %creating an array with the number iterations for NR 57 figure 58 ax = plotyy(plot_iters_nr, res_nr, plot_iters_nr, conv_nr); % x1, yl, x2, y2 59 title(' Newton-raphson' ) 60 xlabel(' iterations') 61 ylabel(ax(1), 'residual' ) 62 ylabel(ax(2), 'convergence' ) 63 grid on 64 65 plot_iters_s = 0:length(res_s)-1; 66 figure 67 ax = plotyy (plot_iters_s, res_s, plot_iters_s, conv_s) ; 68 title( ' secant' ) 69 xlabel( 'iterations' )70 ylabel (ax(1), 'residual') 71 ylabel (ax(2), 'convergence') 72 grid on 73 74 h_nr = (roots_nr (end) ) .*(L / D) .* ((V. ~2) / (2 .* g)); 75 h_s = (roots_s(end) ) .* (L / D) .* ((V. ~2) / (2 .* g)) ; 76 f_nr = (roots_nr(end) ) ; 77 f_s = (roots_s (end) ) ; 78 fprintf("Head Loss using Newton Raphson: %f\ ", h_nr) 79 fprintf("Head Loss using Secant: %f\ ", h_s) 80 fprintf("Friction Factor for Newton Raphson is: "f\ ", f_nr) 81 fprintf( "Friction Factor for Secant is: %f\ ", f_s) H 82 83 %%%Newton_Raphson_Function 84 function [x, conv, res, i] = .. 85 newton_raphson_func (f, df, guess1, conv_tol, res_tol, max_iter) 86 % initial guess 87 x (1) = guess1; 88 % initial convergence 89 conv (1) = 0;90 % initial residual 91 res (1) = abs (f(x(1) ) ) ; 92 93 fprintf('iteration X residual convergence \ ' ) ; 94 fprintf( ' === =\ ' ); 95 fprintf( ' %21 % . 8e % . 4e %. 4e\ ', 0, x(1) , res (1) , conv(1) ) ; 96 97 %initialize the iteration counter 98 i = 1; 99 100 %iterate using our formula 101 while i OneDrive > Documents > EML3034 Matlab labs > #+ Press Ctri + Shift + P) to generate code with Copilot douk dout -130 x(2) = guess2; % second guess for secant method 131 % initial convergence 132 conv (1) = 0; 133 conv (2) = abs (x(2)-x(1) ) ; 134 % initial residual 135 res(1) = abs(f(x(1) ) ) ; 136 res (2) = abs(f(x(2) ) ) ; 137 138 fprintf('iteration X residual convergence \ ' ) ; 139 fprintf ('= == =\ ') ; 140 fprintf(' % 21 % . 8e % _ 4e %% . 4e\ ', 0, x(1) , res (1) , conv (1) ) ; 141 fprintf( ' %21 % . 8e % . 4e % . 4e\ ', 0, x(2) , res (2) , conv(2) ) ; 142 % initialize the iteraion counter 143 i = 2; 144 %iterate using our formula 145 while i

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!