필터 지우기
필터 지우기

Undefined Function or variable..​.NewtonRal​phson

조회 수: 6 (최근 30일)
Corey K
Corey K 2015년 2월 3일
편집: Julia 2015년 2월 3일
Please help! This is the error I am receiving, "Undefined function or variable 'y_value'.
Error in seventh (line 39) while abs(y_value) > accuracy"
And below is the code function x_value = newtonraphson(x0,accuracy)
...
x0 = 10;
accuracy = 1e-10;
x_value = x0;
%subfunction 1
function y_value = find_y(x_value)
y_value = 3*(x_value)^3-15*(x_value)^2-20*(x_value)+50;
%y_value = log(x_value)-x_value+3
end
%subfunction 2
function diff = differentiate_y(x_value)
diff = 9*(x_value)-30*x_value-20;
%diff = 1/x
end
while abs(y_value) > accuracy
xprev = x_value;
x_value = xprev - (y_value)/diff; % where diff is f', the derivative
fprintf('Current x value %d, Current y value %f, Root Approximation %g',xprev,y_value,x_value);
end
disp('The root is: ');
root = x_value;
end

답변 (1개)

Julia
Julia 2015년 2월 3일
편집: Julia 2015년 2월 3일
Hi,
y_value is the output of your first subfunction.
You define the subfunction
function y_value = find_y(x_value)
...
end
and then you have to call it:
y_value = find_y(x_value);
The same holds for diff. You also should update y_value otherwise you will get an endless loop.
But I don't understand why you have the function newtonraphson() with input parameters x0 and accuracy and then the first thing you do is to define x0 and accuracy in the code for this function.

카테고리

Help CenterFile Exchange에서 Discrete Data Plots에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by