Undefined Function or variable..​.NewtonRal​phson

3 次查看(过去 30 天)
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 中查找有关 Geology 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by