please help me to rewrite this code correct
12 views (last 30 days)
Show older comments
i can't run this program and understant how can i debug. please help me .
thanks.
function [ x ] = newtonraphson(x0 ,f ,c)
for i=1:10
x1=x0 - f \diff(f);
x0=x1;
end
if f<c
x=x0;
else
m=1;
while( diff(f, m)==0)
m=m+1;
end
end
while(f<c)
x1=x0-(m-1)*f \diff(f);
end
x=x1;
x0=0.5;
function f=s(x)
s=(x-1)*(x+5)^5;
[x]=newtonraphson(x0 ,'s',0.001);
2 Comments
Answers (3)
Doug Hull
on 21 Apr 2011
Let us do a little test for typical inputs to this line of code:
x1=x0 - f \diff(f);
>> f = [1 2 3]
f =
1 2 3
>> diff(f)
ans =
1 1
x1=x0 - f \diff(f);
So reading this in English
"Set X1 equal to the input variable X0 (of undetermined size) minus the vector of length N backsolve a vector of length (N-1)"
There are a few places this could go wrong, depending on the size of X0, but either way you can not operate on vectors of different lengths like that with the "\" operator.
0 Comments
Matt Tearle
on 21 Apr 2011
A few problems leap out at me right away:
- x1 = x0 - f\diff(f) Slashes in MATLAB are interpreted as "solve the system of equations ...", so f\diff(f) gives the solution (x) to f*x = diff(f). Check the definition of N-R and you'll see that that's not what you want.
- Your first for-loop doesn't achieve anything because f is never updated. You update x0, but not f.
- You are calling the function with f as a string ( 's' ). f needs to be a function reference. In MATLAB, this is done with a function handle: newtonraphson(x0 ,@s,0.001); Then, in your function, f will be a function handle to your function s.m. Otherwise, f will be a character array 's'.
- Check the doc on diff. It isn't taking the derivative -- it is doing differencing on an array. Everything here is a scalar, so diff will return an empty array. (Because you were using a string for f, it was actually calling the symbolic diff function, but then it will return 1 every time, because it interprets 's' as a the function f(s) = s, hence f'(s) = 1.)
- Your definition of s is kinda messed up, too. If you want a symbolic variable, you should do s = sym('(x-1)*(x+5)^5'); Then in your N-R function, you'd need to evaluate it. For that, you could use matlabFunction to convert s (and its derivative) to a function handle.
0 Comments
Musab
on 24 Oct 2023
function regulaFalsi(f,x0,x1,tolerance_value)
%Take equation intervals and tolerance value as argument
%If initail and final interval is given by user then check condition
%f(initial_condition)*f(final_condition) <0
if (f(x0)*f(x1) <0) && (x0 < x1)
counter = 1;
while true
%Formula
x = ((x0 * f(x1)) - (x1 *f(x0))) / (f(x1)-f(x0));
%Check the stoping condition
if abs(f(x))> tolerance_value
if counter == 1
fprintf ("N\tXn-1\t\tf(Xn-1)\t\tXn\t\tf(Xn)\t\tXn+1\t\tf(Xn+1)\n");
end
%Print the value
fprintf ("%d\t%.9f\t%.9f\t%.9f\t%.9f\t%.9f\t%.9f\n",counter,x0,f(x0),x1,f(x1),x,f(x))
%Change the intervales
if (f(x0)*f(x)) < 0
x1 = x;
else
x0 = x;
end
%If the value of function f(x) > tolerance_value stop the process
else
break
end
counter = counter + 1;
end
else
fprintf("Please enter correct intervals");
end
1 Comment
Walter Roberson
on 24 Oct 2023
I do not understand how Regula Falsi code is a solution to a question about Newton Raphson?
See Also
Categories
Find more on Adding custom doc in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!