添加符号变量后很长的数字?

时间:2017-02-14 15:46:08

标签: matlab vector linear-equation

所以我想确定我的SVM分类器的alphabetagamma值,我训练了我的SVM分类器并确定了alpha和支持向量。我的支持向量每行有3个变量,下面只有我支持向量的10行:

0.0904235536887480  -0.269325475875919  -0.678528701392414
-0.321039098061280  -0.507618180664821  -1.42365662798284
-0.0737761304021185 -0.269641311369441  -0.647521877863172
0.00105779420640393 -0.311226557946309  -0.667506146498475
0.0913098589312967  -0.289462325547514  -0.391261050348894
0.00622693949845773 -0.166248587146820  -0.149546793127464
-0.292302915842567  -0.564676268888150  -1.60153093563523
0.112997393643248   -0.310512134534035  -0.725281274142312
-0.135361511770186  -0.456321702624641  -1.26973221898260
-0.173160731078767  -0.434439033384469  -1.22687774941370

以下同样,我的alpha只有十行:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1

所以我基本上做的是确定如下的系数:

c = (-1)*[0.0904*x1 + (-0.2693)*x2 + (-0.6785)*x3] + (-1)*[(-0.321)*x1 + (-0.5076)*x2 + (-1.4236)*x3].....

等等,直到我的alpha

的大小

所以我在Matlab中编写了以下代码

syms x1, syms x2, syms x3;

alpha = SVMStruct.Alpha;
svm_vec = SVMStruct.SupportVectors;

for i = 1:size(alpha,1)
    c(i) = alpha(i)*(svm_vec(i,1)*x1 + svm_vec(i,2)*x2 + svm_vec(i,3)*x3);
end

sum_it = sum(c);

但这创造了一个非常奇怪的输出:

(107845064549358722206080751595348329973204613074833920445585562521882937008164658045489239834546021458299139*x1)/50534761550197893278639420198779799540396107395587434771118149413836407509953624874438129483687080755200 + (95720990302914087945142311872326568914380675701489099929103269189530321664249312169660240242394455632803627*x2)/134178504805697854567421908803656709124500009291732154392279224305703564767807900528680550698065697177600 + (90626366614084720573448362168042659133754200934323766866906741825007634289583081638045482944881264585156183*x3)/125521827076297992982426946945356276277758073208394596044390242092432367040852552107475353878835652198400

这是预期的产量吗?为什么我把它作为分数?

根据输出,我是否正确实现了我的等式?

1 个答案:

答案 0 :(得分:1)

您的计算输入是符号变量,因此使用这些变量的任何计算都将产生 符号变量的精确值。如果您想要结果的近似浮点表示,您需要将总和转换为double

double(sum_it)

为了做到这一点,您需要为x1x2x3

指定实际值
double(subs(sum(c), [x1, x2, x3], [1, 2, 3]))

话虽如此,我认为你实际上想要的是解决方程组。您可以使用\运算符执行此操作

variables = svm_vec \ alpha;