% Shuttle Liftoff Engine Angle % Newton-Raphson Method of iteratively finding a
ID: 1832810 • Letter: #
Question
% Shuttle Liftoff Engine Angle
% Newton-Raphson Method of iteratively finding a single root format long
% Constants
LGB = 4.0; LGS = 24.0; LTS = 38.0;
WS = 0.230E6; WB = 1.663E6;
TB = 5.3E6; TS = 1.125E6;
es = 0.5E-7; nmax = 200;
% Initial estimate in radians
x = 0.25
%Calculation loop
for i=1:nmax
fx = LGB*WB-LGB*TB-LGS*WS+LGS*TS*cos(x)-LTS*TS*sin(x);
dfx = -LGS*TS*sin(x)-LTS*TS*cos(x);
xn=x-fx/dfx;
%convergence check
ea=abs((xn-x)/xn);
if (ea<=es)
fprintf('convergence: Root = %f radians ',xn)
theta = (180/pi)*x;
fprintf('Engine Angle = %f degrees ',theta)
break
end
x=xn;
x
end
I need help writing the same MatLab program using Newton's method but in a different way that ultimately leads me to get the same solutions. So I want the exact same final answers but need another way to write the program.
Explanation / Answer
%%% Extract % % This program finds the roots of a given equation with one independent % variable using Newtons iteration method. The program guesses start values % for the iteration in a given interval. %%% % Following inputs are necessary for the iteration process: % %%% Before running the program % % 1. The equation to be iterated, which must have 'x' and only 'x' as % independent variable. Also you need to set the equation equal to zero. % E.g: sqrt(f)=f <=> sqrt(f)-f=0 is written as y=sqrt(f)-f. % % 2. The interval, which is given by the variables 'xmin' and 'xmax', is % the interval the function is plotted in, and more importantly the interval % in which the program guesses start values for the iteration. % % 3. The 'step' variable gives the discretization of the plot % (points to be plotted per unit). It can be useful to set this value % smaller for some functions (e.g. 0.0001). The reason why is, that the % program after each ended iteration plots the %found value with a cross. % This way it can be seen whether the program have found the root you where % looking for or not. The smaller the 'step' is the more precise the cross % is plotted. For other functions with many roots it might be useful to put % in a higher value (e.g. 0.1) in order to find all the different roots. % %%% After running the program: % % 4. When you run the program you are asked for the number of intersection % points to be found. The program finds the given amounts of min. values % sorted with respect to the x-values. % % 5. The program will now ask you which of the found min. values the % iteration is to be started at. After the iteration has finished % a new start value can be given. This can done as many times as the number % of found min. values. % %%% Outputs % % The program gives following outputs for a finished iteration: % P1: X-values Error % NaN NaN % -1.769300000000000 1.000001000000000 % -1.769292354280612 0.000007645719388 % -1.769292354238631 0.000000000041980 % % X-values gives the x-values, and the last x-value satisfies the given % criteria of decimal accuracy. % Error states the accuracy of the iterated x-value, which is defined as % the absolute value of the difference between the given and the last % x-value. % % It is my hope, that the program will be helpful with your following % iterations :) % % - Jon Andr? Adsersen clc clear all close all % How the program works with variabels % Type 'syms x' to make x a variabel. % To evaluate a function f in a point write: % subs(y,x,x1) syms x %%% Inputs -------------------------------------------------------------%%% y = x^3-2*x + 2 % Equation to be iterated. xmin = -3; % Minimum x value. xmax = 3; % Maximum x value. step = 0.0001; % Steps for the plot. %%%---------------------------------------------------------------------%%% dy = diff(y,x) % The function y derived with respect to the variable x. % Plot af the function y_plot = inline(vectorize(y)); x1 = xmin:step:xmax; y1 = y_plot(x1); hold all plot(x1,y1); plot(x1,x1*0,'color','black'); axis([xmin xmax min(y1) max(y1)]); % Axislimits for the plotwindow. %% Intersection points with the x-axis estimated graphically sp=input('Number of intersection points to be found with the x-axis: '); [sorted_y1 id_y1]= sort(abs(y1)); % The given number of min-values are found. minVals= sorted_y1(1:sp); % Vector with min. y-values. x1_minVals= x1(id_y1(1:sp)); % Vector with matching x-values. [sorted_minVals_x id_minVals_x]= sort(x1_minVals); % The found number of min values is sorted with respect to the x-values. minVals_x= sorted_minVals_x(1:sp); % Vector with min. x-values (sorted). y_minVals_x= minVals(id_minVals_x(1:sp)); % Vector with min. y-values (sorted). if sp==1 display('Intersection point with the x-axis is graphically estimated to:') else display('Intersection points with the x-axis is graphically estimated to:') end for i=1:sp % Sorted min. values. display(['P' num2str(i) ':(' num2str(minVals_x(i)) ',' num2str(y_minVals_x(i)) ')']) end %% Iteration % Newtons iteration method is applied: % x_2 = - f(x_1)/f'(x1) + x_1 deci=5; % Number of decimal accuracy. k=1/10^(deci+1); count=0; while count<sp clear A clear x_v clear error p=input('Start value for the iteration (pointnumber): P'); x_v(2)=minVals_x(p); error(2)=k+1; % The error is given higher than the criteria, so the % following while loop runs at least one time. c=2; % x_v for x-value. while error(c)>k && c<=50 x_v(c+1) = - subs(y,x,x_v(c))/subs(dy,x,x_v(c))+x_v(c); error(c+1)=abs(x_v(c+1)-x_v(c)); c=c+1; end format long scatter(x_v(c-1),0,100,'x','Linewidth',1.5); % Plot of intersection point. A(:,1)=x_v; % Intersection point A(:,2)=error; % Error calculated as the difference between this % intersection point and the last intersection point A(1,1)=NaN; A(1,2)=NaN; disp(['P' num2str(p) ': X-values Error ']) disp(A) count=count+1; end format short newtons_method.m
%%% Extract % % This program finds the roots of a given equation with one independent % variable using Newtons iteration method. The program guesses start values % for the iteration in a given interval. %%% % Following inputs are necessary for the iteration process: % %%% Before running the program % % 1. The equation to be iterated, which must have 'x' and only 'x' as % independent variable. Also you need to set the equation equal to zero. % E.g: sqrt(f)=f <=> sqrt(f)-f=0 is written as y=sqrt(f)-f. % % 2. The interval, which is given by the variables 'xmin' and 'xmax', is % the interval the function is plotted in, and more importantly the interval % in which the program guesses start values for the iteration. % % 3. The 'step' variable gives the discretization of the plot % (points to be plotted per unit). It can be useful to set this value % smaller for some functions (e.g. 0.0001). The reason why is, that the % program after each ended iteration plots the %found value with a cross. % This way it can be seen whether the program have found the root you where % looking for or not. The smaller the 'step' is the more precise the cross % is plotted. For other functions with many roots it might be useful to put % in a higher value (e.g. 0.1) in order to find all the different roots. % %%% After running the program: % % 4. When you run the program you are asked for the number of intersection % points to be found. The program finds the given amounts of min. values % sorted with respect to the x-values. % % 5. The program will now ask you which of the found min. values the % iteration is to be started at. After the iteration has finished % a new start value can be given. This can done as many times as the number % of found min. values. % %%% Outputs % % The program gives following outputs for a finished iteration: % P1: X-values Error % NaN NaN % -1.769300000000000 1.000001000000000 % -1.769292354280612 0.000007645719388 % -1.769292354238631 0.000000000041980 % % X-values gives the x-values, and the last x-value satisfies the given % criteria of decimal accuracy. % Error states the accuracy of the iterated x-value, which is defined as % the absolute value of the difference between the given and the last % x-value. % % It is my hope, that the program will be helpful with your following % iterations :) % % - Jon Andr? Adsersen clc clear all close all % How the program works with variabels % Type 'syms x' to make x a variabel. % To evaluate a function f in a point write: % subs(y,x,x1) syms x %%% Inputs -------------------------------------------------------------%%% y = x^3-2*x + 2 % Equation to be iterated. xmin = -3; % Minimum x value. xmax = 3; % Maximum x value. step = 0.0001; % Steps for the plot. %%%---------------------------------------------------------------------%%% dy = diff(y,x) % The function y derived with respect to the variable x. % Plot af the function y_plot = inline(vectorize(y)); x1 = xmin:step:xmax; y1 = y_plot(x1); hold all plot(x1,y1); plot(x1,x1*0,'color','black'); axis([xmin xmax min(y1) max(y1)]); % Axislimits for the plotwindow. %% Intersection points with the x-axis estimated graphically sp=input('Number of intersection points to be found with the x-axis: '); [sorted_y1 id_y1]= sort(abs(y1)); % The given number of min-values are found. minVals= sorted_y1(1:sp); % Vector with min. y-values. x1_minVals= x1(id_y1(1:sp)); % Vector with matching x-values. [sorted_minVals_x id_minVals_x]= sort(x1_minVals); % The found number of min values is sorted with respect to the x-values. minVals_x= sorted_minVals_x(1:sp); % Vector with min. x-values (sorted). y_minVals_x= minVals(id_minVals_x(1:sp)); % Vector with min. y-values (sorted). if sp==1 display('Intersection point with the x-axis is graphically estimated to:') else display('Intersection points with the x-axis is graphically estimated to:') end for i=1:sp % Sorted min. values. display(['P' num2str(i) ':(' num2str(minVals_x(i)) ',' num2str(y_minVals_x(i)) ')']) end %% Iteration % Newtons iteration method is applied: % x_2 = - f(x_1)/f'(x1) + x_1 deci=5; % Number of decimal accuracy. k=1/10^(deci+1); count=0; while count<sp clear A clear x_v clear error p=input('Start value for the iteration (pointnumber): P'); x_v(2)=minVals_x(p); error(2)=k+1; % The error is given higher than the criteria, so the % following while loop runs at least one time. c=2; % x_v for x-value. while error(c)>k && c<=50 x_v(c+1) = - subs(y,x,x_v(c))/subs(dy,x,x_v(c))+x_v(c); error(c+1)=abs(x_v(c+1)-x_v(c)); c=c+1; end format long scatter(x_v(c-1),0,100,'x','Linewidth',1.5); % Plot of intersection point. A(:,1)=x_v; % Intersection point A(:,2)=error; % Error calculated as the difference between this % intersection point and the last intersection point A(1,1)=NaN; A(1,2)=NaN; disp(['P' num2str(p) ': X-values Error ']) disp(A) count=count+1; end format short Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.