how to filter out decimals

4 Ansichten (letzte 30 Tage)
Osman Ballhysa
Osman Ballhysa am 4 Mai 2021
Bearbeitet: Jan am 5 Mai 2021
function s = Fnkt(n)
syms k x
s = symsum(k,k,0,n);
if s<=0
s = 0;
end
Can somebody help me figure out, how do i filter out decimals ?
Example:
Fnkt(2) , then i will get 1+2=3
for negative numbers i want to get 0 .
But how do i get rid of decimals, for example Fnkt (2.5) or neg. decimals (-2.5) to get also a zero ?

Akzeptierte Antwort

Jan
Jan am 4 Mai 2021
Bearbeitet: Jan am 5 Mai 2021
function s = Fnkt(n)
if n <= 0 || n ~= floor(n)
s = 0;
return;
end
syms k x
s = symsum(k,k,0,n);
end
Do you realy want to calculate this symbolically? What about:
function s = Fnkt(n)
if n <= 0 || n ~= floor(n)
s = 0;
else % sum(1:n) with Gauss' method:
s = n * (n + 1) / 2;
end
end
  2 Kommentare
Osman Ballhysa
Osman Ballhysa am 4 Mai 2021
thank you , oohhh i see. it could also work with round(n) instead of floor(n)
Osman Ballhysa
Osman Ballhysa am 4 Mai 2021
yeah i was just messing around and trying, symsum was probabily not the smartest way to do it

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Symbolic Math Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by