Please write a script for summation of 1/k

5 Ansichten (letzte 30 Tage)
Deleena Rebello
Deleena Rebello am 24 Jan. 2022
Kommentiert: Deleena Rebello am 25 Jan. 2022
  2 Kommentare
KSSV
KSSV am 24 Jan. 2022
What is your attempt for the simple home work?
Deleena Rebello
Deleena Rebello am 24 Jan. 2022
N=...;
sum = 0.0;
for k=1:N
sum = sum+1/k;
end
Iam new in this . So need a complete script please.
This is the question

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

DGM
DGM am 24 Jan. 2022
I don't see why the code you wrote wouldn't work.
N = 100;
s = 0; % using sum as a name shadows the function sum()
for k = 1:N
s = s + 1/k;
end
s
s = 5.1874
That works, but you can always do it without the loop.
s = sum(1./(1:N))
s = 5.1874
  3 Kommentare
Deleena Rebello
Deleena Rebello am 25 Jan. 2022
It is showing an error
Undefined function or method 'summation' for input arguments of type 'char'.
(summation is my file name) This is the error
DGM
DGM am 25 Jan. 2022
Attach your file and an example of how you're calling it.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 25 Jan. 2022
Is summation.m a function or a script? If it's what you posted already, it's a script and should not give any error about inputs since there are no inputs. And it should work, once you rename sum to x, as DGM showed in his answer, which ran it in MATLAB online.
On the other hand, if it's a function the first line will be
function summation(N)
and maybe you're supposed to call it passing in N and maybe all you did was click the green run triangle without assigning N. Again, attach your m-file. Use the paperclip icon.
  2 Kommentare
Deleena Rebello
Deleena Rebello am 25 Jan. 2022
N=input('N=');
s=0.0;
for k=1:N
s=s+1/k;
end
this Mfile works
Deleena Rebello
Deleena Rebello am 25 Jan. 2022
Thank you all

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Mathematics 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