Newton's method problem

clear all; close all;
a=4;
b=3;
x=zeros();
x(1)=1;
for i=1:1000;
f(x(i))=(x(i)-a)^2+b;
x(i+1) =x(i)-(f(x(i))/diff(f(x(i))));
end

댓글 수: 4

Grzegorz Knor
Grzegorz Knor 2011년 10월 7일
Could you describe a problem that you try to solve using this code?
Andrei Bobrov
Andrei Bobrov 2011년 10월 7일
Hi Grzegorz! This is method Newton?
Prozka
Prozka 2011년 10월 7일
Yeah
Actually I need to write code for solve optimization problem
using Newtons Method
the problem is
Minimizing the f(x)=(x-4)^2+9
Walter Roberson
Walter Roberson 2011년 10월 7일
Code that has a "clear all" statement is broken 99 times out of 100.

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

답변 (3개)

Andrei Bobrov
Andrei Bobrov 2011년 10월 7일

1 개 추천

for your case EDITED
f = @(x)(x - 4)^2 + 9;
syms x
ex = (x - 4)^2 + 9;
cf = double(coeffs(ex));
distxp = (4*prod(cf([1 end]))-cf(2)^2)/4/cf(3);
fun = matlabFunction(ex - distxp);
df = matlabFunction(diff(ex));
x = 10;
xout = x;
while abs(fun(x)) > 1e-6
x1 = x - fun(x)/df(x);
x = x1;
xout = [xout;x];
end
ADD corrected
f = @(x)(x - 4)^2 + 9;
syms x
ex = (x - 4)^2 + 9;
cf = fliplr(double(coeffs(expand(ex))));
distxp = (4*prod(cf([1 end]))-cf(2)^2)/4/cf(1);
fun = @(x)f(x)-distxp;
dcf = polyder(cf);
df = @(x)polyval(dcf,x);
x = 10;
xout = x;
while abs(fun(x)) > 1e-6
x1 = x - fun(x)/df(x);
x = x1;
xout = [xout;x];
end
output for the first 10 iterations and optimal x
if numel(xout)<=10
out = xout;
else
out = [xout(1:10);xout(end)];
end

댓글 수: 7

Prozka
Prozka 2011년 10월 7일
How do i get output for the first 10 iterations and optimal x value and solution?
Prozka
Prozka 2011년 10월 9일
I am getting error saying that
??? Error using ==> sym.maple at 87
Error, invalid arguments to coeffs
Error in ==> sym.coeffs>coeffsAndTerms at 56
c = maple('coeffs',p,x,'''__ans''');
Error in ==> sym.coeffs at 39
[c,t] = coeffsAndTerms(p,indets);
Error in ==> edited at 4
cf = double(coeffs(ex));
Andrei Bobrov
Andrei Bobrov 2011년 10월 9일
corrected
Prozka
Prozka 2011년 10월 9일
thanks
but i need to have iterations run times let say 10000
but output only first 10 iterations of x value and f(x) value
Andrei Bobrov
Andrei Bobrov 2011년 10월 9일
outf = f(out(end))
Andrei Bobrov
Andrei Bobrov 2011년 10월 9일
Proshka, include your head (brain) 10000 iterations is very many
Prozka
Prozka 2011년 10월 9일
above case I agree with you we need 10 iterations
but I need to use this code for complex functions
like f(x)=cosx+sin2x+e-x
thanks

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

Andreas Goser
Andreas Goser 2011년 10월 7일

0 개 추천

This code isn't working because diff(f(x(i))) returns [] and thus a scalar can't be divide by [].

댓글 수: 1

Prozka
Prozka 2011년 10월 7일
Actually I need to write code for solve optimization problem
using Newtons Method
the problem is
Minimizing the f(x)=(x-4)^2+9

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

Steve
Steve 2011년 10월 9일

0 개 추천

You can use this Newton function implementation in general.
function n = newton(f,fp,x0,tol,Nmax)
n=0;
test_val = abs(poly_val(f,x0));
num1 = 1;
while (test_val > tol && num1<Nmax)
n = x0 - poly_val(f,x0)/poly_val(fp,x0);
test_val = abs(poly_val(f,n));
x0=n;
num1=num1+1;
end
end

카테고리

도움말 센터File Exchange에서 Nonlinear Optimization에 대해 자세히 알아보기

질문:

2011년 10월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by