please help me to rewrite this code correct
이전 댓글 표시
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
Matt Fig
2011년 4월 21일
Please go back and format your code using the '{} Code' button!
Sean de Wolski
2011년 4월 21일
And report to us any error messages you see in full
답변 (3개)
Doug Hull
2011년 4월 21일
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.
Matt Tearle
2011년 4월 21일
0 개 추천
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.
Musab
2023년 10월 24일
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
Walter Roberson
2023년 10월 24일
I do not understand how Regula Falsi code is a solution to a question about Newton Raphson?
카테고리
도움말 센터 및 File Exchange에서 Adding custom doc에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!