在Matlab中使用solve代替vpasolve来加速计算?

在Matlab中使用solve代替vpasolve来加速计算?,matlab,numeric,Matlab,Numeric,我对Matlab比较陌生。目前,我正在用vpasolve解一个方程。因为这需要循环80多个变量,所以速度很慢 % input variables % ffsample = Fourier transformed reference waveform % ffref = Fourier transformed reference waveform len=80; n1=1; n2=1.95; alpha_grad=30; beta_grad=14.858; % constants tp12=2

我对Matlab比较陌生。目前,我正在用vpasolve解一个方程。因为这需要循环80多个变量,所以速度很慢

% input variables
% ffsample = Fourier transformed reference waveform
% ffref = Fourier transformed reference waveform

len=80;
n1=1;
n2=1.95;
alpha_grad=30;
beta_grad=14.858;

% constants
tp12=2.*n1.*cos(alpha)./(n2.*cos(alpha)+n1.*cos(beta));
tp21=2.*n2.*cos(beta)./(n1.*cos(beta)+n2.*cos(alpha));

%functions with unknown n3
syms n3
gamma= asin(n2.*sin(beta)./n3);
rp23 = (n3 .* cos(beta) - n2.*cos(gamma)) ./ (n3 .* cos(beta) + n2 .* cos(gamma));

t = cputime;
res_para=zeros(len,1);

%loop to solve for n3
digits(5):
for i=1:len
    res_para(i)=vpasolve(ffsample(i)./ffref(i) == (tp12*tp21*rp23) ./ (tp12*tp21), n3);
end

e = cputime-t

%convert results to complex numbers
res_para=double(res_para);
res_para=squeeze(res_para);
res_para=res_para';
现在,我试着把它转换成一个方程,在进入循环之前使用solve。据我所知,我应该结合vpa使用solve?目前,我正在尝试以下代码,但无法启动:

res_p=solve(x == (tp12*tp21*rp23) ./ (tp12*tp21), n3);

t = cputime;
res_para=zeros(len,1);

for i=1:len

    x=ffsample(i)./ffref(i);
    res_para=vpa(res_p(x));
end
任何帮助都将不胜感激

使现代化 谢谢你的回复。我改变了数码的位置。 是的,Digit5确实是一个很低的值,但是它将上述代码的速度提高了大约3倍

正如我所说,我对matlab比较陌生,我认为先以符号方式解方程,然后插入变量将大大缩短时间,但也许我在这里错了

考虑到我使用vpasolve数值求解一个非常简单的方程:

function [ xval ] = example(  )
syms x
for y=1:10
    xval(y) = vpasolve(y == 5.*x+6, x);
end
如果我先用x的符号解函数y=5x+6,然后在循环中包含msising值,不是更快吗?大概是这样的:

function [ xval ] = example2(  )
syms x
% symbolic solve for x (???)
% y=5x+6 ==> x=(y-6)/5
sol=solve( y == 5.*x+6, x)

for y=1:10
    xval(y) = sol(y);
end

显然,示例2不起作用

它无法启动->你试过按开始键吗?或者除了无法启动之外,还有其他问题吗?错误是:错误使用sym>检查索引行1545索引必须是正整数或逻辑。。据我所知,vpa是一种符号函数。但我实际想要的是从给定的x的解中解出n3的方程。调试您的代码并检查x的值inres\u para=vpares\u px;。最有可能发生的事情是x不是正整数。使用可变精度/符号有什么原因吗?如果你的目标是速度,不要使用符号数学,而是用数字来实现。为什么你认为解决会更快?请修复代码中的错误,使其可运行。您不需要在每个循环中设置数字,因为迭代5是一个非常低的值。但是,完成后,您确实需要将其设置回默认值。