2014-12-21 2 views
0

Newton 메서드를 사용하여 함수의 근원을 찾아야합니다. 키보드에서 간격과 정확도를 입력합니다. 내가 컴파일 할 때 여기 내 코드행렬 차원이 일치해야 함, Newton 메서드

disp('Newton method') 
[email protected](g) 5*sin(g.^3-2*g.^2-1); 
[email protected](g) 5*g*(3*g-4)*cos(-g.^3+2*g.^2+1); 
fx2=inline('-5*((4-6*g)*cos(-g.^3+2*g.^2+1)-(4*g-3*g.^2).^2*sin(-g.^3+2*g.^2+1))'); 
e=input ('Enter accuracy:'); 
a=input ('enter a:'); 
b=input ('enter b:'); 
x0=a:e:b; 
y= 5*sin(x0.^3-2*x0.^2-1); 
y2= -5*((4-6*x0)*cos(-x0.^3+2*x0.^2+1)-(4*x0-3*x0.^2).^2*sin(-x0.^3+2*x0.^2+1)); 
plot (x0,y),grid 
xlabel('x'),ylabel('y') 
fa=fx(a); 
n=0; 
if (fa*y2>0) 
    x1=a; 
else 
    x1=b; 

end; 
while(abs(fx(x1))>e) 
     n=n+1; 
    x1=x1-(fx(x1))/(fx1(x1)); 

end; 
disp(sprintf('Answer:%g',x1)) 
    disp(sprintf('Number of iterations:%g',n)) 

, 그것은 말한다 :

Error using * 
Inner matrix dimensions must agree. 

Error in Untitled3 (line 10) 
y2= -5*((4-6*x0)*cos(-x0.^3+2*x0.^2+1)-(4*x0-3*x0.^2).^2*sin(-x0.^3+2*x0.^2+1)); 

답변

1

당신은이 1xn 벡터를 곱하여하는 것은, 즉 수 없습니다. 이 곱셈 오류가 발생합니다 해결책이 될 수있는 요소 현명한 곱셈 .*를 사용

y2= -5*((4-6*x0)*cos(-x0.^3+2*x0.^2+1)-(4*x0-3*x0.^2).^2*sin(-x0.^3+2*x0.^2+1)); 
       ^         ^
       |          | 

,하지만 난 당신이 구현하려고 모르겠어요.

+0

대니얼 감사합니다, 오류가 사라 졌어요하지만 그것은 나에게 대답을 준다 - 그 동안 뭔가해야합니다. -0.864746 ... – DNilla

+0

왜 그 번호를 기대합니까? – Daniel

관련 문제