I have the idea but my syntax is horrible can someone please help I need this is
ID: 3552352 • Letter: I
Question
I have the idea but my syntax is horrible can someone please help I need this is is key for practice and understanding for this one I ONLY NEED PROBLEM 4!!!!
One method for finding the approximate root(s) of a equation, f(x)=0 is the Newton-Raphson method and given as, xk-1 = xk-f(xk)/f'(xk),k=1,2,3, where xk-1 is the current approximation, xk is the previous approximation. Write MATLAB program for finding a root of any equation, f(x)=0 given the function (use inline), the initial guess, and a pre-specified tolerance (see below). The program output is the approximate value of the root of the equation, Do not allow the program to conduct more than 50 iterations. If the loop terminated by conducting all 50 iterations, provide an appropriate error message to the user. If the value is less than or equal to the pre-specified tolerance, the tolerance is considered to have been met.Explanation / Answer
% This program works for given function, if you want to do it for anothere fuction, change function.
% This programme is to find the solution of a non linear equation by Newton Raphson method.
% This is not a generalized program
clc;
clear all;
format long;
syms x;
e = 1e-5; % setting the tolerance value
dx = e + 1;
f = log(2-x) + x^2; % enter your function here;
x = 7; % initially assumed value of x
count = 0; % setting counter to know the no of iterations taken
p = zeros(1,1);
while (abs(dx) > e && count <51) % initialising the iteration and continue until the error is less than tolerance
dx = eval(f/(diff(f))); % calculating dx, diff is used for finding the differentiation of the fuction
x = x - dx % updating the value of x
count = count + 1; % incrimenting the counter
p(count) = x;
drawnow();
plot(abs(p),'r','linewidth',3);
grid;
if (count == 50)
fprintf('Error...! Solution not converging !!! '); % printing the error message
break;
end
end
% plot(abs(p));
if (count < 300)
fprintf('The solution = '); %printing the result
x
fprintf(' Number of iteration taken = %d ',count);
end