Removal of For Loops
2 次查看(过去 30 天)
显示 更早的评论
Is it possible to script the following function in MATLAB without a for loop, or any other iterative loop? I am trying to understand if it is possible to have a matrix reference itself as it is populated using a single command in MATLAB. Thanks for looking!
x(1)=0;
x(2)=1;
x(3)=2;
for n=4:51
x(n)=x(n-1) + x(n-3);
end
0 个评论
采纳的回答
Sean de Wolski
2012-2-1
It's probably possible with filter() or similar. But I guarantee a well written for-loop will not be much slower. Just remember to preallocate x.
x = zeros(51,1);
%etc.
0 个评论
更多回答(1 个)
Walter Roberson
2012-2-1
I = sqrt(-1);
x = @(n) -(301/14945472)*((1+I*3^(1/2))*(93^(1/2)-93/7)*(108+12*93^(1/2))^(1/3)-744/7-(5/14)*(-1+I*3^(1/2))*(93^(1/2)-31/5)*(108+12*93^(1/2))^(2/3))*(((1/72)*(-93^(1/2)+9)*(108+12*93^(1/2))^(2/3)+(1/6)*(108+12*93^(1/2))^(1/3))^n*(((I*3^(1/2)-67/43)*93^(1/2)+279/43-((775/43)*I)*3^(1/2))*(108+12*93^(1/2))^(1/3)+((-((97/129)*I)*3^(1/2)+27/43)*93^(1/2)+((248/43)*I)*3^(1/2)-310/43)*(108+12*93^(1/2))^(2/3)+1860/43+((92/43)*I)*3^(1/2)*93^(1/2))*((1/72)*(-9+93^(1/2))*(-1+I*3^(1/2))*(108+12*93^(1/2))^(1/3)-(1/72)*(108+12*93^(1/2))^(2/3)-((1/72)*I)*(108+12*93^(1/2))^(2/3)*3^(1/2)+1/3)^n+(6696/43)*(-(1/72)*(-9+93^(1/2))*(1+I*3^(1/2))*(108+12*93^(1/2))^(1/3)-(1/72)*(108+12*93^(1/2))^(2/3)+((1/72)*I)*(108+12*93^(1/2))^(2/3)*3^(1/2)+1/3)^n*((1/72)*(-93^(1/2)+9)*(108+12*93^(1/2))^(2/3)+(1/6)*(108+12*93^(1/2))^(1/3))^n+((-((12/43)*I)*3^(1/2)+98/43)*93^(1/2)-1302/43-((248/43)*I)*3^(1/2))*(108+12*93^(1/2))^(1/3)+((-((89/129)*I)*3^(1/2)+35/43)*93^(1/2)+((279/43)*I)*3^(1/2)-217/43)*(108+12*93^(1/2))^(2/3)+1860/43-((92/43)*I)*3^(1/2)*93^(1/2))/((1/72)*(-93^(1/2)+9)*(108+12*93^(1/2))^(2/3)+(1/6)*(108+12*93^(1/2))^(1/3))^n
But watch out for floating point round-off.
(Yes, really. And yes, this is the simplified form of the expression.)
2 个评论
Walter Roberson
2012-2-1
Note: the above was produced by simplifying the output of Maple's rsolve() routine. The basic form of the answer is not very complicated, but it involves the sum of terms with the sum taken over the roots of a cubic expression, and that cubic happens to have two imaginary solutions. The expanded expression before simplification is pretty grotty.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!