Trying to answer a math problem using newton's method but having problem in the function.

조회 수: 1 (최근 30일)
This is the last function when newton's method is applied:
I used z=log(x) in my function to make it easier to understand:
clc
x=0.25;
y=0;
while (y-x)<10^-4
z=log10(x);
y = x-((((x ^ 2)z-(x ^ 0.5)-20)2.x ^0.5)/(-1+4.x ^ (3/2)z+2.x ^ (3/2)))
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets instead of parentheses.
x=y;
end

채택된 답변

Alan Stevens
Alan Stevens 2023년 3월 17일
Matlab doesn't like implied multiplication
(x ^ 2)z
should be
(x ^ 2)*z
etc.
Also, in Matlab
log(x)
is ln(x)

추가 답변 (2개)

Cris LaPierre
Cris LaPierre 2023년 3월 17일
You need to include the multiplication operator when coding your equation. Also, it is '*', not '.'. Nothing is inferred.
I think your conditional will be problematic, since the last line of your loop sets x equal to y.
x=0.25;
y=0;
while (y-x)<10^-4
z=log10(x);
y = x-((((x ^ 2)*z-(x ^ 0.5)-20)*2*x ^0.5)/(-1+4*x ^ (3/2)*z+2*x ^ (3/2)))
x=y;
end
  댓글 수: 1
Cris LaPierre
Cris LaPierre 2023년 3월 17일
편집: Cris LaPierre 2023년 3월 17일
+1 about log10 vs log for natural ln.
Also wanted to point out that you have created an infinite loop. Since x=y, (y-x) is 0, which is <1e-4. Since your condition will always be true, your loop will never end.
Just changing the < to > will prevent the infinite loop, but won't fix the issue that y=x. Consider creating a third variable to prevent this.
Also consider the case that the difference of (y-x) may be positive or negative. You may want your condition to compare to the absolute value of this difference.

댓글을 달려면 로그인하십시오.


Voss
Voss 2023년 3월 17일
Multiplication is * or .* but never . as in 4.x and never implicit as in (x^2)z
log10 is base-10 logarithm. log is natural logarithm.
z = log(x);
y = x-((((x^2)*z-(x^0.5)-20)*2*x^0.5)/(-1+4*x^(3/2)*z+2*x^(3/2)));

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by