eq = 'x^4 - 7*x^3 + 3*x^2 - 5*x + 9 = 0';
s = solve(eq);
disp('The first root is: '), disp(s(1));
disp('The second root is: '), disp(s(2));
disp('The third root is: '), disp(s(3));
disp('The fourth root is: '), disp(s(4));
%将根转换为double类型
disp('Numeric value of first root'), disp(double(s(1)));
disp('Numeric value of second root'), disp(double(s(2)));
disp('Numeric value of third root'), disp(double(s(3)));
disp('Numeric value of fourth root'), disp(double(s(4)));
v = [1, -7, 3, -5, 9];
s = roots(v);
%将根转换为double类型
disp('Numeric value of first root'), disp(double(s(1)));
disp('Numeric value of second root'), disp(double(s(2)));
disp('Numeric value of third root'), disp(double(s(3)));
disp('Numeric value of fourth root'), disp(double(s(4)));
当使用许多符号函数时,应声明变量是符号变量,但是Octave定义符号变量的方法不同。注意使用 Sin 和 Cos ,它们也在符号包中定义。
创建一个脚本文件并输入以下代码-
%首先,加载包,确保它已安装。
pkg load symbolic
%使symbols模块可用
symbols
%定义符号变量
x = sym ('x');
y = sym ('y');
z = sym ('z');
%扩展方程
expand((x-5)*(x+9))
expand((x+2)*(x-3)*(x-5)*(x+7))
expand(Sin(2*x))
expand(Cos(x+y))
%收集方程式
collect(x^3 *(x-7), z)
collect(x^4*(x-3)*(x-5), z)
运行文件时,它显示以下结果-
ans =
-45.0+x^2+(4.0)*x
ans =
210.0+x^4-(43.0)*x^2+x^3+(23.0)*x
ans =
sin((2.0)*x)
ans =
cos(y+x)
ans =
x^(3.0)*(-7.0+x)
ans =
(-3.0+x)*x^(4.0)*(-5.0+x)