Is MATLAB confusing functions and arrays?
显示 更早的评论
I have no idea what's going wrong with this code. I'm writing a simple function to find the root of a quadratic function using Newton's method:
function xNext = newtonMethod(x0, f)
xNext = x0;
for i = 1:20
func = f(xNext);
dx = diff(f(xNext));
xNext = xNext - (func / dx);
end
end
This may seem a bit convulted, but I've gone through several versions of this trying to figure out where I was going wrong. No matter what I do, I get the error message:
Error using /
Matrix dimensions must agree.
It seems to me like MATLAB is considering func and dx to be matrices when they're really just variables, but I'm not really sure. Any advice would be greatly appreciated. This has been a real head-scratcher for me.
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Common Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!