在 scilab 中找到方程的最小根
Find the smallest root of the equation in scilab
我需要使用 scilab 找到方程的最小绝对根,精度为 0.00001。
方程本身:x - cos (1.04 * x) = 0。
需要建立一个图形来确定函数改变符号的时间间隔。然后计算一阶和二阶导数。需要确定它们的符号,所有符号都应该相同(*)。
If1, then the calculation is performed according to the following formulas1.
if2, then the calculation is performed according to the following formulas2.
计算结束when.
如何在 scilab 中实现所有这些?
好的,所以我尝试在 matlab 中执行此操作。但我仍然不确定所有内容是否正确组合,以及如何将其传输到 scilab?
clc;
clear;
syms x
f = x - cos(1.04*x);
a=0.5;
b=1;
eps=0.00001;
i=0;
c=(a+b)/2;
f1=diff(f);
f2=diff(f1);
while(abs(b-a)>eps)
if((subs(f1,x,c)*subs(f2,x,c))>=0)
a=a-(b-a)*subs(f,x,a)/(subs(f,x,b)-subs(f,x,a));
b=b-subs(f,x,b)/subs(f1,x,b);
else
a=a-subs(f,x,a)/subs(f1,x,a);
b=b-(b-a)*subs(f,x,b)/(subs(f,x,b)-subs(f,x,a));
end
i=i+1;
end
fprintf('b=% f \n', double(b))
ezplot(f,[0.5 1]),hold on
plot(b,subs(f,x,b),'or')
grid on
这是我在 scilab 中的内容。
clc;
clear;
a=0.5;
b=1;
deff ("y = f (x)", "y = x-cos (1.04 * x)")
deff ("y = f1(x)", "y = 1.04.*sin(1.04*x)+1")
deff ("y = f2(x)", "y = 1.0816.*cos(1.04*x)")
eps=0.00001;
i=0;
c=(a+b)/2;
m=0;
com1 = ["k a b absolute f(a) f(b) f1(b)"];
tab = [];
while(abs(b-a)>eps)
if((f1(c)*f2(c))>=0)
a=a-(b-a)*f(a)/(f(b)-f(a));
b=b-f(b)/f1(b);
else
a=a-f(a)/f1(a);
b=b-(b-a)*f(b)/(f(b)-f(a));
end
i=i+1;
m=abs(b-a);
tab = [tab; i a b m f(a) f(b) f1(b)];
end
disp('b=', double(b))
disp(tab)
fprintfMat("table.txt", tab, "%1.15f", com1)
fplot2d(-10:0.1:10,f)
plot(b,f(b),'or')
xgrid
不存在 Matlab ezplot
的等效命令,但接近的命令是 fplot2d
。要添加网格,您有 xgrid
.
clc;
clear;
a=0.5;
b=1;
deff ("y = f (x)", "y = x-cos (1.04 * x)")
deff ("y = f1(x)", "y = 1.04.*sin(1.04*x)+1")
deff ("y = f2(x)", "y = 1.0816.*cos(1.04*x)")
eps=0.00001;
i=0;
c=(a+b)/2;
tab = [a,b];
while(abs(b-a)>eps)
if((f1(c)*f2(c))>=0)
a=a-(b-a)*f(a)/(f(b)-f(a));
b=b-f(b)/f1(b);
else
a=a-f(a)/f1(a);
b=b-(b-a)*f(b)/(f(b)-f(a));
end
tab = [tab; a b];
i=i+1;
end
disp('b=% f \n', double(b))
disp(tab)
fprintfMat("table.txt",tab)
fplot2d(-10:0.1:10,f)
plot(b,f(b),'or')
xgrid
图像已使用 `xs2png(0,"graph.png") 从 Scilab 导出:
我需要使用 scilab 找到方程的最小绝对根,精度为 0.00001。 方程本身:x - cos (1.04 * x) = 0。 需要建立一个图形来确定函数改变符号的时间间隔。然后计算一阶和二阶导数。需要确定它们的符号,所有符号都应该相同(*)。
If1, then the calculation is performed according to the following formulas1.
if2, then the calculation is performed according to the following formulas2.
计算结束when.
如何在 scilab 中实现所有这些?
好的,所以我尝试在 matlab 中执行此操作。但我仍然不确定所有内容是否正确组合,以及如何将其传输到 scilab?
clc;
clear;
syms x
f = x - cos(1.04*x);
a=0.5;
b=1;
eps=0.00001;
i=0;
c=(a+b)/2;
f1=diff(f);
f2=diff(f1);
while(abs(b-a)>eps)
if((subs(f1,x,c)*subs(f2,x,c))>=0)
a=a-(b-a)*subs(f,x,a)/(subs(f,x,b)-subs(f,x,a));
b=b-subs(f,x,b)/subs(f1,x,b);
else
a=a-subs(f,x,a)/subs(f1,x,a);
b=b-(b-a)*subs(f,x,b)/(subs(f,x,b)-subs(f,x,a));
end
i=i+1;
end
fprintf('b=% f \n', double(b))
ezplot(f,[0.5 1]),hold on
plot(b,subs(f,x,b),'or')
grid on
这是我在 scilab 中的内容。
clc;
clear;
a=0.5;
b=1;
deff ("y = f (x)", "y = x-cos (1.04 * x)")
deff ("y = f1(x)", "y = 1.04.*sin(1.04*x)+1")
deff ("y = f2(x)", "y = 1.0816.*cos(1.04*x)")
eps=0.00001;
i=0;
c=(a+b)/2;
m=0;
com1 = ["k a b absolute f(a) f(b) f1(b)"];
tab = [];
while(abs(b-a)>eps)
if((f1(c)*f2(c))>=0)
a=a-(b-a)*f(a)/(f(b)-f(a));
b=b-f(b)/f1(b);
else
a=a-f(a)/f1(a);
b=b-(b-a)*f(b)/(f(b)-f(a));
end
i=i+1;
m=abs(b-a);
tab = [tab; i a b m f(a) f(b) f1(b)];
end
disp('b=', double(b))
disp(tab)
fprintfMat("table.txt", tab, "%1.15f", com1)
fplot2d(-10:0.1:10,f)
plot(b,f(b),'or')
xgrid
不存在 Matlab ezplot
的等效命令,但接近的命令是 fplot2d
。要添加网格,您有 xgrid
.
clc;
clear;
a=0.5;
b=1;
deff ("y = f (x)", "y = x-cos (1.04 * x)")
deff ("y = f1(x)", "y = 1.04.*sin(1.04*x)+1")
deff ("y = f2(x)", "y = 1.0816.*cos(1.04*x)")
eps=0.00001;
i=0;
c=(a+b)/2;
tab = [a,b];
while(abs(b-a)>eps)
if((f1(c)*f2(c))>=0)
a=a-(b-a)*f(a)/(f(b)-f(a));
b=b-f(b)/f1(b);
else
a=a-f(a)/f1(a);
b=b-(b-a)*f(b)/(f(b)-f(a));
end
tab = [tab; a b];
i=i+1;
end
disp('b=% f \n', double(b))
disp(tab)
fprintfMat("table.txt",tab)
fplot2d(-10:0.1:10,f)
plot(b,f(b),'or')
xgrid
图像已使用 `xs2png(0,"graph.png") 从 Scilab 导出: