How do i fix my newtons method code?

2 次查看(过去 30 天)
Matlab1
Matlab1 2023-11-9
评论: Matlab1 2023-11-9
Hello, can someone pls help me fix my code? im not sure which part is wrong and how to correct it. I appreciate any help. Thank you for your time.

回答(2 个)

Torsten
Torsten 2023-11-9
编辑:Torsten 2023-11-9
fx and df are not function handles in your code. So you must use "subs" to evaluate them at a certain x-value.
And setting x0 = x1 before you test whether abs(x1-x0) < 1e-6 is wrong. In this case, since x1 = x0, abs(x1-x0) is always < 1e-6, namely 0.
syms x
fx = x^3 - 3*x^2 + 3*x - 9;
fplot(x,fx); grid on
fxroot = solve(fx);
fxroot
df = 3*x^2 - 6*x + 3;
x0 = 2.5;
n = 20;
for i = 1:n
x1 = eval(x0 - subs(fx,x,x0)/subs(df,x,x0));
if abs(x1 - x0) < 1e-6
break
end
x0 = x1;
end

Steven Lord
Steven Lord 2023-11-9
There is one obvious syntax error, one functionality error, and one change I strongly recommend.
Recommended change
On the line where you call eval, do not call eval on a symbolic expression. [You should avoid calling eval anywhere on general principles but certainly don't call it on a symbolic expression.] If you need the symbolic result to be converted to a number, call double on it instead. That is the change I strongly recommend.
Syntax error
On that same line of code you have a syntax error. Let's count parentheses, starting at 0 at the start of the line. Add 1 when you see a ( and subtract 1 when you see a ). If the count at the end of the line is not 0, you have mismatched parentheses, more ( than ). If the count ever goes negative, you have more ) than ( and that's an error as well.
x1 = eval(x0 - fx(x0)/df(x0);
% 0 1 2 1 2 1
You're missing a ). Fix that when you replace eval with double. That's the syntax error.
Note that if you open the file in the MATLAB Editor, Code Analyzer will flag that line with a red error message indicating a problem that likely will prevent your code from executing and will tell you (in a shorter sentence) what I said.
Functionality error
Once you do that, MATLAB will still complain on this line. You may expect fx(x0) to evaluate the function fx at x0 but fx is not a function. It is a symbolic expression. To substitute values into a symbolic expression use the subs function.
subs(fx, x, x0)
Alternately you could convert the symbolic expression into an anonymous function using the matlabFunction function. Anonymous functions can be evaluated using that syntax. Note that if you do this you won't be able to perform symbolic-only operations (like diff, which you could have used to generate df from fx) on the anonymous function.
M = matlabFunction(fx);
M(x0) % works
That fixes the functionality error.
Suggestion for the future
One suggestion for the future: when asking for help here with code that "doesn't work", throws an error, or issues a warning, please provide information about the specific behavior that's not working as you expect or is throwing an error. Including the full and exact text of the error and/or warning messages (all the text displayed in red or orange) will help with identifying the cause of the error and/or warning. The easier you make it for people to investigate the more likely they'll be to help.
  1 个评论
Matlab1
Matlab1 2023-11-9
Hello, thank you for the reply and the suggestion! I will keep it in mind in the future :)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Assumptions 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by