我看不到牛顿法的 Matlab 代码输出,我错过了什么?
I can't see the output of my Matlab code for Newton's Method, what am I missing?
所以我有以下 matlab 代码,我试图计算 Newton's Method 来求解非线性方程(特别是确定平方根)。我看不到我的两个 fprintf() 语句的输出,所以我想我从根本上误解了 MATLAB 调用函数的方式。
我觉得这里缺少一些非常基本的东西。
我是 运行 这个 Matlab 网络应用 https://matlab.mathworks.com/
我的代码如下:
clear all
p = 81; %set to whatever value whose square root you want to find.
egFun = @ (x) x^2 - p;
egFunDer = @ (x) 2*x;
firstGuess = p;
Err = 0.00001;
imax = 20;
%% (+) function, functionDervivate, X-estimate, Error, i-Maximum (-) X-Solution
function Xsolution = NewtonRoot(Fun, FunDer, Xest, Err, imax)
for i = 1 : imax
fprintf("test %f", i)
% below is the newton's method formula in action
Xi = Xest - Fun(Xest)/FunDer(Xest)
% let Xest be replaced with our new value for X so that we can
% perform the next iteration
if(abs(0-Fun(Xi)) <= Err)
Xsolution = Xi;
break
end
Xest = Xi
end
Xsolution = Xi;
end
%%
function answer = main
answer = NewtonRoot(egFun, egFunDer, firstGuess, Err, imax)
fprintf("the Solution is %f", answer)
end
我希望看到 answer 的值,它应该打印到控制台,以及我为了调试而输入的两个 fprintf() 语句。
需要在主代码中调用NewtonRoot函数
clear all
p = 81; %set to whatever value whose square root you want to find.
egFun = @ (x) x^2 - p;
egFunDer = @ (x) 2*x;
firstGuess = p;
Err = 0.00001;
imax = 20;
%%
answer = NewtonRoot(egFun, egFunDer, firstGuess, Err, imax)
fprintf("the Solution is %f", answer)
%% (+) function, functionDervivate, X-estimate, Error, i-Maximum (-) X-Solution
function Xsolution = NewtonRoot(Fun, FunDer, Xest, Err, imax)
for i = 1 : imax
fprintf("test %f", i)
% below is the newton's method formula in action
Xi = Xest - Fun(Xest)/FunDer(Xest)
% let Xest be replaced with our new value for X so that we can
% perform the next iteration
if(abs(0-Fun(Xi)) <= Err)
Xsolution = Xi;
break
end
Xest = Xi
end
Xsolution = Xi;
end
所以我有以下 matlab 代码,我试图计算 Newton's Method 来求解非线性方程(特别是确定平方根)。我看不到我的两个 fprintf() 语句的输出,所以我想我从根本上误解了 MATLAB 调用函数的方式。
我觉得这里缺少一些非常基本的东西。
我是 运行 这个 Matlab 网络应用 https://matlab.mathworks.com/
我的代码如下:
clear all
p = 81; %set to whatever value whose square root you want to find.
egFun = @ (x) x^2 - p;
egFunDer = @ (x) 2*x;
firstGuess = p;
Err = 0.00001;
imax = 20;
%% (+) function, functionDervivate, X-estimate, Error, i-Maximum (-) X-Solution
function Xsolution = NewtonRoot(Fun, FunDer, Xest, Err, imax)
for i = 1 : imax
fprintf("test %f", i)
% below is the newton's method formula in action
Xi = Xest - Fun(Xest)/FunDer(Xest)
% let Xest be replaced with our new value for X so that we can
% perform the next iteration
if(abs(0-Fun(Xi)) <= Err)
Xsolution = Xi;
break
end
Xest = Xi
end
Xsolution = Xi;
end
%%
function answer = main
answer = NewtonRoot(egFun, egFunDer, firstGuess, Err, imax)
fprintf("the Solution is %f", answer)
end
我希望看到 answer 的值,它应该打印到控制台,以及我为了调试而输入的两个 fprintf() 语句。
需要在主代码中调用NewtonRoot函数
clear all
p = 81; %set to whatever value whose square root you want to find.
egFun = @ (x) x^2 - p;
egFunDer = @ (x) 2*x;
firstGuess = p;
Err = 0.00001;
imax = 20;
%%
answer = NewtonRoot(egFun, egFunDer, firstGuess, Err, imax)
fprintf("the Solution is %f", answer)
%% (+) function, functionDervivate, X-estimate, Error, i-Maximum (-) X-Solution
function Xsolution = NewtonRoot(Fun, FunDer, Xest, Err, imax)
for i = 1 : imax
fprintf("test %f", i)
% below is the newton's method formula in action
Xi = Xest - Fun(Xest)/FunDer(Xest)
% let Xest be replaced with our new value for X so that we can
% perform the next iteration
if(abs(0-Fun(Xi)) <= Err)
Xsolution = Xi;
break
end
Xest = Xi
end
Xsolution = Xi;
end