【发布时间】:2014-12-23 18:30:24
【问题描述】:
我正在尝试在 Matlab 中编写 Newton Raphson 方法,但首先我必须找到我的函数的导数。我是 Matlab 的新手,我没有得到我需要的结果。我尝试了不同的方法,但没有任何帮助,我想我没有得到我想要的结果,因为我不熟悉 Matlab 语法。这是我的代码:
1) 首先,我将我的函数放在一个名为 fun1.m 的文件中
function y = fun1(x)
y = exp(x) - 2*x - 2;
2) 然后我转到我尝试编写 Newton Raphson 方法的另一个文件
%my bounds
low = 0;
high = 3;
%my initial guess will be determined from product of the function of the file fun1.m and the derivative of that function for the boundaries that i've given. If one of the products is greater then zero than this value will be my first guess. This is what I wrote
f = fun1(x);
f1 = diff(f);
f2 = diff(f,2);
%As a result if I put out the semicolons I get f1 = 0 and f2 = 0
%I want to compute these products
prod1 = f(low) * diff(f(low))
prod2 = f(high) * diff(f(high))
我应该怎么做?我也尝试过文件句柄,结果在区分后我得到了[]。也没有必要将函数放在另一个文件中,但是因为我必须做 3 个使用相同函数的方法,所以我认为从文件中获取函数而不是每次都编写它会更好。那么如何获取函数并设置低值和高值并区分它们呢?
【问题讨论】:
标签: matlab