function methods = prj2() a = input('enter the function', 's'); f = inline(a); itr = 0; lim = 0.00001; c = input('enter the method number, 1 for secant, 2 for newton, 3 for QII'); error = 1; while (error>lim) i = 1; if (c == 1) x(1) = input('enter the first guess'); x(2) = input('enter the second guess'); while (error>lim) x(i+2)=x(i+1)-(f(x(i+1))*(x(i+1)-x(i))/(f(x(i+1))-f(x(i)))); i = i + 1; error = x(i+1)-x(i); itr = itr + 1 root = x(i+1) end elseif (c == 2) x(1) = input('enter the initial x'); while (error > lim) x(i+1)=x(i)-((f(x(i)))/diff(f(x(i)))); error = abs(x(i+1)-x(i)); itr = itr + 1; i = i + 1; root = x(i); end elseif (c == 3) x(1) = input('enter the first x'); x(2) = input('enter the second x'); x(3) = input('enter the third x'); while (error > lim) a = ((f(x(i+1))*f(x(i)))/((f(x(i+2))-(f(x(i+1))))*(f(x(i+2))-f(x(i)))))*(x(i+2)); b = ((f(x(i+2))*f(x(i)))/((f(x(i+1))-(f(x(i+2))))*(f(x(i+1))-f(x(i)))))*(x(i+1)); c = ((f(x(i+1))*f(x(i+2)))/((f(x(i))-(f(x(i+1))))*(f(x(i))-f(x(i+2)))))*(x(i)); x(i+3) = a + b + c; i = i + 1; error = abs(x(i+1)-x(i)); itr = itr + 1 root = x(i) end else disp(sprintf('no')); end end
거의 처음 매트랩으로 숙제하는데요 (미분 과목이에요) 매트랩으로 과제를 받아서요... line 20에 보시면 else if (c==2)가 있는데요 그 문장에 들어가는 공식의 분모부분을 보시면 diff(f(x(i)))) 부분이 있는데요 유저가 입력하는 f 함수의 미분식을 계산하게 해야하는데 알고리듬이나 기본 메소드를 사용해서 자동적으로 계산을 못할까요?