Filter löschen
Filter löschen

Removal of For Loops

1 Ansicht (letzte 30 Tage)
Ben
Ben am 1 Feb. 2012
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

Akzeptierte Antwort

Sean de Wolski
Sean de Wolski am 1 Feb. 2012
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.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 1 Feb. 2012
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 Kommentare
Sean de Wolski
Sean de Wolski am 1 Feb. 2012
nice! +1 for sure.
Walter Roberson
Walter Roberson am 1 Feb. 2012
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.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by