[問題] Input argument "x" is undefined已回收
在 command window 輸入 main 之後,會出現下面的訊息
??? Input argument "x" is undefined.
Error in ==> main at 2
y = x^2 - 2*x + 1;
那想請問,我應該要在哪邊定義 x?
感謝回答
以下是 main.m 的程式碼 (照著老師上課寫在黑板的)
%用二分法求出一元多次方程式的根
function y=f(x)
y = x^2 - 2*x + 1;
bisection(@f, 1e-8, 1000, 0, 2);
end
以下是 bisection.m 的內容 (老師提供的)
function bisection(f, tolerance, maxIter, a, b)
i = 1;
qa = f(a); qb = f(b);
if qa*qb > 0,
disp('Bracket error!');
return;
end
while i <= maxIter,
p = 0.5*(a+b);
q = f(p);
disp(sprintf('n = %3d, p = %+0.16e ', i, p));
if abs(q) < tolerance,
break;
end
i = i + 1;
if (q*qa > 0),
a = p; qa = q;
else
b = p; qb = q;
end
end
if ( i > maxIter),
disp('Fail after maximum number of iterations.');
end
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.36.233.227
推
09/29 22:25, , 1F
09/29 22:25, 1F
→
09/29 22:25, , 2F
09/29 22:25, 2F
→
09/29 22:32, , 3F
09/29 22:32, 3F
推
09/29 22:33, , 4F
09/29 22:33, 4F
→
09/29 22:33, , 5F
09/29 22:33, 5F
→
09/29 22:35, , 6F
09/29 22:35, 6F
→
09/29 22:40, , 7F
09/29 22:40, 7F
MATLAB 近期熱門文章
PTT數位生活區 即時熱門文章