Power series with array input

i is the imaginary unit
i is the imaginary unit
r is a vector of length n: [r(1),...,r(n)]
phi is a 1x300 double, i.e. [phi(1),...,phi(300)]
sum(r(1:n).*(1i.^(1:n))./factorial(1:n))
This would work if there was no phi. But how can I implement the phi here?
sum(r(1:n).*((phi*1i).^(1:n))./factorial(1:n))
results in:
Matrix dimensions must agree.
The expected output is the same size as phi. This code would achieve what I want but I want n to be dynamic so the looping is not feasible:
if n==1
R = r(1) * ( i * phi )
elseif n==2
R = r(1) * ( i * phi ) + r(2) * ( i * phi ).^2 / 2;
elseif n==3
R = r(1) * ( i * phi ) + r(2) * ( i * phi ).^2 / 2 + r(3) * ( i * phi ).^3 / 6;

1 commentaire

Walter Roberson
Walter Roberson le 2 Mar 2022
"r is a vector of length n: [r(1),...,r(n)]"
Make it a column vector instead of a row vector.

Connectez-vous pour commenter.

Réponses (2)

William Rose
William Rose le 2 Mar 2022
Modifié(e) : William Rose le 2 Mar 2022
[edit: delete a line of unnecessary code]
I assume that, since you have 300 values of phi, that you want to evaluate this sum 300 times - once for each value of phi.
I would make an array with 300 columns and n rows. Then I would add up the elements in each column.
n=20; m=300;
r=rand(n,1); phi=rand(1,m);
a=zeros(n,m);
for i=1:n
a(i,:)=r(i)*(1i*phi).^i/factorial(i);
end
b=sum(a);
fprintf('Size(a)=%d by %d. Size(b)=%d by %d.\n',size(a),size(b));
Size(a)=20 by 300. Size(b)=1 by 300.
Try.

1 commentaire

William Rose
William Rose le 2 Mar 2022
Inspection of the array a generated by the code above shows that the odd rows are purely imaginary and the even rows are purely real. This makes sense for the equation you provided, since i raised to an odd power is imaginary, and i raised to an even power is real.
My code would have been better if I had used k instead of i as the loop index. Using i as the loop index invites confusion between the index variable and 1i=sqrt(-1). But Matlab is smart, and it did what I intended.

Connectez-vous pour commenter.

I am not certain what the desired final result is, or what the function does.
If ‘n’ and ‘r’ are row vectors (and by definition the same size), this works —
n = 1:10;
r = randn(size(n));
phi = randn(1,300);
f = sum(r(1:n).*((phi(:)*1i).^(1:n))./factorial(1:n), 2)
f =
0.0000 + 2.5522i 0.0000 - 0.2423i 0.0000 - 0.0800i 0.0000 + 1.0025i 0.0000 + 2.3643i 0.0000 + 1.9902i 0.0000 - 0.9255i 0.0000 - 0.4978i 0.0000 + 0.3187i 0.0000 - 0.4999i 0.0000 + 1.4970i 0.0000 + 0.4513i 0.0000 - 1.5100i 0.0000 + 0.3980i 0.0000 - 0.3492i 0.0000 - 1.2289i 0.0000 + 0.1888i 0.0000 - 0.4520i 0.0000 + 1.0414i 0.0000 + 0.1794i 0.0000 + 0.8659i 0.0000 - 1.0742i 0.0000 - 1.6944i 0.0000 - 0.2593i 0.0000 - 0.8256i 0.0000 + 1.1879i 0.0000 + 0.8879i 0.0000 - 1.1257i 0.0000 - 2.1923i 0.0000 + 0.6300i 0.0000 + 0.1149i 0.0000 - 0.9278i 0.0000 - 1.2232i 0.0000 + 1.9374i 0.0000 - 0.8060i 0.0000 - 0.0442i 0.0000 - 0.6165i 0.0000 + 0.6103i 0.0000 - 1.3000i 0.0000 - 0.1428i 0.0000 + 2.3213i 0.0000 + 1.3499i 0.0000 - 1.7218i 0.0000 + 0.2126i 0.0000 + 0.6803i 0.0000 - 0.2899i 0.0000 + 0.9384i 0.0000 + 1.7098i 0.0000 + 0.0836i 0.0000 + 1.4234i 0.0000 - 1.3953i 0.0000 - 0.8173i 0.0000 - 1.1096i 0.0000 - 0.2573i 0.0000 - 1.7330i 0.0000 + 0.0025i 0.0000 + 0.9547i 0.0000 + 1.2956i 0.0000 + 0.2665i 0.0000 + 0.3642i 0.0000 - 0.4498i 0.0000 + 1.8262i 0.0000 - 0.0848i 0.0000 - 2.4847i 0.0000 - 1.1836i 0.0000 - 1.8076i 0.0000 + 1.9944i 0.0000 + 2.0694i 0.0000 - 1.9263i 0.0000 + 2.8552i 0.0000 - 1.6130i 0.0000 + 1.0521i 0.0000 + 0.2911i 0.0000 + 1.6470i 0.0000 + 1.6287i 0.0000 - 2.1730i 0.0000 - 0.8024i 0.0000 + 0.9954i 0.0000 - 0.3227i 0.0000 + 0.2338i 0.0000 - 0.0417i 0.0000 - 0.7468i 0.0000 + 0.6966i 0.0000 + 2.0760i 0.0000 - 0.4989i 0.0000 + 1.4312i 0.0000 - 1.4751i 0.0000 - 0.0497i 0.0000 + 0.5478i 0.0000 + 0.5496i 0.0000 - 0.5810i 0.0000 + 2.3686i 0.0000 + 1.0115i 0.0000 - 2.1794i 0.0000 - 0.5671i 0.0000 + 0.9703i 0.0000 - 0.7234i 0.0000 - 0.1337i 0.0000 - 0.2653i 0.0000 + 0.3497i 0.0000 - 0.2099i 0.0000 - 0.6035i 0.0000 - 0.9160i 0.0000 - 0.3010i 0.0000 - 1.0486i 0.0000 + 1.5382i 0.0000 + 0.2989i 0.0000 + 0.3764i 0.0000 - 1.5675i 0.0000 - 2.1321i 0.0000 + 1.2074i 0.0000 - 1.2140i 0.0000 - 0.3104i 0.0000 + 0.9429i 0.0000 - 1.4976i 0.0000 - 0.5853i 0.0000 - 0.8267i 0.0000 - 1.4596i 0.0000 - 1.0727i 0.0000 + 0.3395i 0.0000 + 1.8331i 0.0000 - 2.1187i 0.0000 + 1.5836i 0.0000 + 1.4864i 0.0000 - 1.9867i 0.0000 + 0.8905i 0.0000 - 1.1027i 0.0000 + 2.5253i 0.0000 - 0.7602i 0.0000 - 1.1583i 0.0000 + 1.5662i 0.0000 + 3.0098i 0.0000 - 1.8510i 0.0000 - 1.6193i 0.0000 + 2.5035i 0.0000 - 0.4486i 0.0000 + 0.2913i 0.0000 + 1.6380i 0.0000 + 1.0891i 0.0000 + 0.8659i 0.0000 + 0.5175i 0.0000 - 1.9701i 0.0000 - 1.2983i 0.0000 + 1.2489i 0.0000 + 0.9517i 0.0000 - 0.7264i 0.0000 - 1.9402i 0.0000 + 1.3512i 0.0000 - 0.6550i 0.0000 + 2.0431i 0.0000 - 0.2754i 0.0000 - 0.3759i 0.0000 + 1.3297i 0.0000 - 1.1363i 0.0000 - 0.3223i 0.0000 + 1.9556i 0.0000 + 0.1679i 0.0000 - 0.6252i 0.0000 + 0.6146i 0.0000 + 1.5533i 0.0000 - 2.4313i 0.0000 - 0.4451i 0.0000 - 0.1006i 0.0000 + 0.9021i 0.0000 + 0.6846i 0.0000 - 2.1336i 0.0000 + 0.8069i 0.0000 + 0.5698i 0.0000 + 0.2423i 0.0000 + 1.5452i 0.0000 - 0.1786i 0.0000 - 0.6177i 0.0000 - 1.2681i 0.0000 - 1.9042i 0.0000 - 0.7548i 0.0000 - 1.9682i 0.0000 - 1.4640i 0.0000 + 0.2513i 0.0000 + 0.5511i 0.0000 - 0.6675i 0.0000 - 0.9154i 0.0000 - 0.4660i 0.0000 + 0.3779i 0.0000 - 0.5208i 0.0000 + 1.7249i 0.0000 - 1.7544i 0.0000 - 0.0905i 0.0000 - 0.4624i 0.0000 - 0.5758i 0.0000 - 0.8223i 0.0000 - 1.0661i 0.0000 + 0.9876i 0.0000 + 0.3400i 0.0000 + 0.1780i 0.0000 + 0.1472i 0.0000 - 1.0055i 0.0000 - 1.4601i 0.0000 + 0.1234i 0.0000 - 0.0993i 0.0000 - 1.0555i 0.0000 - 1.9373i 0.0000 + 0.4138i 0.0000 - 0.3250i 0.0000 - 2.4456i 0.0000 - 0.0366i 0.0000 + 1.1124i 0.0000 + 0.9459i 0.0000 + 0.5782i 0.0000 + 1.5291i 0.0000 - 1.1238i 0.0000 - 0.5743i 0.0000 - 0.8920i 0.0000 + 2.1274i 0.0000 - 1.3948i 0.0000 + 1.1165i 0.0000 - 0.4190i 0.0000 - 0.4922i 0.0000 + 0.8814i 0.0000 + 1.6054i 0.0000 - 0.0516i 0.0000 - 1.1375i 0.0000 + 1.0663i 0.0000 - 2.9803i 0.0000 + 1.1112i 0.0000 - 1.4042i 0.0000 - 0.2183i 0.0000 + 0.9384i 0.0000 + 0.8272i 0.0000 - 0.8359i 0.0000 + 0.5610i 0.0000 + 0.0448i 0.0000 - 0.2700i 0.0000 - 0.8075i 0.0000 + 0.1635i 0.0000 + 1.0263i 0.0000 + 1.0830i 0.0000 - 1.5579i 0.0000 - 1.4517i 0.0000 - 1.5125i 0.0000 + 2.8066i 0.0000 - 1.2860i 0.0000 + 2.1999i 0.0000 - 0.9952i 0.0000 - 1.4798i 0.0000 + 1.8944i 0.0000 - 0.0761i 0.0000 + 1.2244i 0.0000 - 0.3821i 0.0000 + 2.0044i 0.0000 - 1.7948i 0.0000 - 2.2205i 0.0000 + 0.7722i 0.0000 - 0.0858i 0.0000 - 2.1664i 0.0000 - 0.1130i 0.0000 + 0.4830i 0.0000 - 0.1489i 0.0000 + 2.0927i 0.0000 + 0.3926i 0.0000 + 0.9144i 0.0000 + 1.6810i 0.0000 - 0.3192i 0.0000 - 2.1349i 0.0000 + 1.0623i 0.0000 + 3.5124i 0.0000 - 0.5646i 0.0000 + 1.1828i 0.0000 + 1.6064i 0.0000 + 1.1362i 0.0000 - 0.2746i 0.0000 - 1.1054i 0.0000 - 0.8546i 0.0000 + 1.8484i 0.0000 - 0.8683i 0.0000 + 1.6728i 0.0000 + 0.5616i 0.0000 + 3.0425i 0.0000 + 0.0420i 0.0000 + 0.3419i 0.0000 + 2.0992i 0.0000 - 0.2963i 0.0000 + 1.8775i 0.0000 - 1.2685i 0.0000 - 1.0253i 0.0000 + 3.2209i 0.0000 - 1.0786i 0.0000 - 1.0181i 0.0000 - 1.4171i 0.0000 - 1.8769i 0.0000 - 1.2774i 0.0000 + 1.2115i 0.0000 + 0.1073i 0.0000 - 1.2173i 0.0000 + 0.2910i 0.0000 + 1.8746i 0.0000 - 1.0409i 0.0000 + 0.0872i 0.0000 - 1.2077i 0.0000 + 0.3463i 0.0000 - 0.7635i
Convert ‘phi’ to be a column vector, and then take the sum across the columns, providing an initial column vector as the output as a funciton of ‘phi’. This uses vector-matrix multiploication to produce the vectorised result. Then use sum on that result, or a column sum on the original vector without first taking the row sum, if only a scalar final result is desired.
.

Catégories

En savoir plus sur MATLAB dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by