Summation with looping
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Daniel
am 5 Dez. 2011
Kommentiert: OLUBUKOLA ogunsola
am 5 Jun. 2016
Find ∑((x^)n/n!) for x=1.8 and n=0 to 16.
Okay, my first attempt to do this looked like:
x = 1.8 For(0:16) xs = sum((x^n)/factorial(n)) end
xs
But I received the error: ??? Error: File: imageanalystbs.m Line: 11 Column: 6 Unexpected MATLAB expression.
My line 11 is the one with "For (0:16)"
What am I doing wrong here? Any help is appreciated.
0 Kommentare
Akzeptierte Antwort
Paulo Silva
am 5 Dez. 2011
for n=0:16 is the correct syntax, you are also making another error by doing the summation in every iteration, you should instead find all values and do the summation after the loop ends
x = 1.8
xl=zeros(17,1); %preallocation of the storage
for n=0:16
xl(n+1) = ((x^n)/factorial(n)); %save every iteration result
end
xs=sum(xl) %calculate the sum of the iteration results
it can also be done without loops
n=0:16;
xs = sum(((x.^n)./factorial(n))); %notice the dots
6 Kommentare
OLUBUKOLA ogunsola
am 5 Jun. 2016
i hope someone will still answer this after a long time. how can i achieve this without the built in function factorial, also ill like x to vary from say , 1 to 1000?
Weitere Antworten (1)
Image Analyst
am 6 Dez. 2011
I think the problem is in your filename:
"imageanalystbs.m"
That sounds like a horrible name to me.
ImageAnalyst
2 Kommentare
Salah Saad
am 5 Mai 2015
Bearbeitet: Salah Saad
am 5 Mai 2015
he's being sarcastic...ish, it still is a weird filename
Siehe auch
Kategorien
Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!