Filter löschen
Filter löschen

Summation of vectors: use of sum function in loop

1 Ansicht (letzte 30 Tage)
jacob Mitch
jacob Mitch am 28 Okt. 2019
Kommentiert: Shubham Gupta am 30 Okt. 2019
Say I have a a series %%(1/t)\sqrt{\sum_{k=1}^{s}(vector^t+vectortwo^t)^2}%% or (1/t)*sqrt of (sigma from t=1 to s) (vector^t+vectortwo^t)^3
Im tyring to do a summation of this but I'm not sure how i'm doing I have tried
vector=[1;2;3;4;5;6];
vectortwo=[1;3;4;5;6;8];
s=length(vector);
count=0;
for t=1:s
Value=sum(vector.^t+vectortwo.^t).^3
Newvalue=(1/t)*sqrt(Value);
count=count+Value;
end
Have I made an error somewhere in for loop

Akzeptierte Antwort

Shubham Gupta
Shubham Gupta am 29 Okt. 2019
Bearbeitet: Shubham Gupta am 29 Okt. 2019
I think
Newvalue=(1/t)*sqrt(Value);
should be outside the for loop :
vector=[1;2;3;4;5;6];
vectortwo=[1;3;4;5;6;8];
s=length(vector);
total=zeros(size(vector)); % change the name of the count
for t=1:s
Value=(vector.^t+vectortwo.^t).^3
total=total+Value;
end
NewValue = (1/s)*sqrt(total); % Output is 6x1 vector
Also, your mentioned series seems to be mathematically incorrect, since 't' can never be out of the "sigma". So, my understanding is it should be 's' instead of 't'
  2 Kommentare
jacob Mitch
jacob Mitch am 29 Okt. 2019
yes sorry you are correct, it was a typo with s. Thank you so much
Shubham Gupta
Shubham Gupta am 30 Okt. 2019
Glad I could help, if it solved your question, please consider accepting the answer

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements 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