Is MATLAB confusing functions and arrays?
Show older comments
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.
Accepted Answer
More Answers (0)
Categories
Find more on Common Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!