clc clear all %% Exercise 4 - Chapter 24 disp('Exercise 4 - Chapter 24') disp('INPUTS') % PURPOSE: Prompt the user to enter a vector of elements % INPUTS/CODE user_vec(1)=input(' enter the first element '); % first element for i=2:1:inf % for an infinte vector x=input(' enter the next element '); % prompt the next element if x=='over' % did the user type 'over' (as a string)? break % if true, end loop end user_vec(i)=x; % to generate the vector end disp('OUTPUTS') disp(' The vector, as entered') disp(user_vec') % to display the vector %% Exercise 2 - Chapter 25 disp('Exercise 2 - Chapter 25') % PURPOSE: Find the zeros of an entered function % NOTE 1: This method only finds one zero of the function, other zeros may exist % NOTE 2: This method may diverge to infinity % NOTE 3: This method only finds real zeros % INPUTS disp('INPUTS') syms x % symbolic variable eqn ='x^2-6'; % function to solve x for when equal to zero - any function will work g =-34; % inital guess - the closer the better fprintf(' The function is %s\n',char(eqn)) fprintf(' The inital guess is %g\n\n',g) % CODE deqn=diff(eqn,x,1); % finding deqn/dx deqn=char(deqn); % inline function requires strings f =inline(eqn); % using inline to form f(x) df =inline(deqn); % using inline to form f'(x) for i=1:1:50 % conducting 50 reps, which is probably over-done g=g-f(g)/df(g); % formula provided end % OUTPUTS disp('OUTPUTS') fprintf(' One estimated root of the function is %g\n\n',g) disp(' The exact roots of the function are:') disp(double(solve(eqn,x)))