matlab secant method
2015-06-05 09:57
543 查看
% Matlab script to illustrate the secant method
% to solve a nonlinear equation
% this particular script finds the square root of a number M
% (input by the user)
% note that the function we are trying to zero is f(x) = x^2 - M.
% this function is hard-coded in the script.
g=9.8065;
k=0.00341;
% f(x)=log(cosh(t*srt(g*k)))/k;
format long
% get user input
M = input('Please enter the number whose square root you want: ')
t0 = input('Please enter the first of two starting guesses: ')
t1 = input('Please enter the second of two starting guesses: ')
% iteration counter
k = 1
% compute first secant iterate to enter loop
s = (((log(cosh(t1*sqrt(g*k)))/k)-M)-((log(cosh(t0*sqrt(g*k)))/k)-M) )/(t1-t0);
% s = ( (x1^2-M) - (x0^2-M) ) / (x1 - x0);
t = t1 - (((log(cosh(t1*sqrt(g*k)))/k)-M))/s
% x = x1 - (x1^2-M)/s
disp('Hit return to continue')
pause
while abs(t-t1) > eps*abs(t),
% reset guesses
t0 = t1;
t1 = t;
% increment iteration counter
k = k + 1
% compute and display secant iterate
s = (((log(cosh(t1*sqrt(g*k)))/k)-M)-((log(cosh(t0*sqrt(g*k)))/k)-M) )/(t1-t0);
% s = ( (x1^2-M) - (x0^2-M) ) / (x1 - x0);
% x = x1 - (x1^2-M)/s
t = t1 - (((log(cosh(t1*sqrt(g*k)))/k)-M))/s
disp('Hit return to continue')
pause
end
% to solve a nonlinear equation
% this particular script finds the square root of a number M
% (input by the user)
% note that the function we are trying to zero is f(x) = x^2 - M.
% this function is hard-coded in the script.
g=9.8065;
k=0.00341;
% f(x)=log(cosh(t*srt(g*k)))/k;
format long
% get user input
M = input('Please enter the number whose square root you want: ')
t0 = input('Please enter the first of two starting guesses: ')
t1 = input('Please enter the second of two starting guesses: ')
% iteration counter
k = 1
% compute first secant iterate to enter loop
s = (((log(cosh(t1*sqrt(g*k)))/k)-M)-((log(cosh(t0*sqrt(g*k)))/k)-M) )/(t1-t0);
% s = ( (x1^2-M) - (x0^2-M) ) / (x1 - x0);
t = t1 - (((log(cosh(t1*sqrt(g*k)))/k)-M))/s
% x = x1 - (x1^2-M)/s
disp('Hit return to continue')
pause
while abs(t-t1) > eps*abs(t),
% reset guesses
t0 = t1;
t1 = t;
% increment iteration counter
k = k + 1
% compute and display secant iterate
s = (((log(cosh(t1*sqrt(g*k)))/k)-M)-((log(cosh(t0*sqrt(g*k)))/k)-M) )/(t1-t0);
% s = ( (x1^2-M) - (x0^2-M) ) / (x1 - x0);
% x = x1 - (x1^2-M)/s
t = t1 - (((log(cosh(t1*sqrt(g*k)))/k)-M))/s
disp('Hit return to continue')
pause
end
相关文章推荐
- matlab生成圆
- Matlab图像处理系列2———空间域平滑滤波器
- MATLAB squeeze 函数
- matlab 常用函数汇总
- 双车道元胞自动机交通流Matlab仿真
- Matlab图像处理系列1———线性变换和直方图均衡
- Matlab在Mac下使用libsvm配置
- 朴素贝叶斯算法matlab实现以及EM算法
- matlab中的xcorr 自相关函数(转)
- 在Windows下编译CAFFE并使用其matlab和python接口
- Stanford公开课机器学习---week2-2.Matlab/Octave 快速教程
- Matlab中sortrows函数解析
- 对于运行时间很长的matlab程序能否察看其运行进度
- K均值聚类(K-means)算法及应用(matlab)
- Matlab之统计函数
- 影像卷积和滤波运算(高斯滤波模板)
- matlab中for循环的循环变量在循环体中不能改变的问题
- matlab在macbook下的详细配置以及可能遇到的所有问题的解决方式(success testing)
- matlab下配置libsvm
- Matlab符号微积分练习