Undefined Function or variable..​.NewtonRal​phson

7 ビュー (過去 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.

Community Treasure Hunt

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

Start Hunting!

Translated by