[英]MATLAB help required function root
我做了一个程序
function bisection;
x1=input('enter the first value=')
x2=input('enter the second value=')
%f3=[];
for x=1:20
%x=1;
x3=(x1+x2)/2;
while x3-x1 >= 0.001
f3(x)=x3^3 + x3^2 - 3*x3 - 3;
f1(x)=x1^3 + x1^2 - 3*x1 - 3;
if ((f3(x)*f1(x)) < 0)
x2=x3;
else
x1=x3;
end
break
end
format long
f3'
disp('The root is found to be =');
x3
end
。 。 。 。 。 程序计算用户给定的间隔(x1,x2)的函数,
我的程序可以编译和执行,但是很少重复执行愚蠢的重复操作,直到循环完成为止,我想在使用条件时达到所需值时停止进一步的打印循环。
二分法是通过以下方法实现的。 将要分析的功能以及间隔边界作为参数传递。
function bisection(f, x1, x2)
if f(x1)*f(x2) < 0 % check precondition sign(f(x1)*f(x2)) = -1
while abs(x2-x1) >= 0.001
x3=(x1+x2)/2;
if ((f(x1)*f(x3)) < 0)
x2 = x3;
elseif ((f(x2)*f(x3)) < 0)
x1 = x3;
else
break
end
end
fprintf('The root is found to be = %.3f\n', x3);
else
fprintf('f(x1) and f(x2) must have opposite signs!\n')
end
寻根任务的亚军部分。
format long
f = @(x) x^3 + x^2 - 3*x - 3;
x1=input('enter the first value=');
x2=input('enter the second value=');
bisection(f, x1, x2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.