필터 지우기
필터 지우기

create For Loop for a summation

조회 수: 3 (최근 30일)
Giovanni Curiazio
Giovanni Curiazio 2022년 11월 3일
댓글: Giovanni Curiazio 2022년 11월 10일
Hello, I need to write a code in matlab for the following summation:
Where m_i and DeltaQ_d are randomly created arrays. I created the following code but I think it is wrong
N=100
D=rand(N,1,'single'); % vettore detriti di 100 numeri casuali tra 0 e 1
somma=sum(D);
m_i=(D/somma)*Mv; %massa casuale dei detriti
ver=sum(m_i); % verifica che la somma dei dei 100 pezzi razionalizzati restituisce il peso del velivolo
vx_i = randn(N,1,"single"); % componenti random di velocità nelle tre direzioni
vy_i = randn(N,1,"single");
vz_i= randn(N,1,"single");
DeltaV_id= [vx_i,vy_i,vz_i]; % matrice velocità i-esima detriti
DeltaQ_err=0;
for k=1:N
T=sum(m_i.*DeltaV_id)
DeltaQ_err= DeltaQ_err+T(k)
end
DeltaQ_err
  댓글 수: 1
Suman
Suman 2022년 11월 8일
Hi Giovanni,
Can you please provide the value of "Mv"?

댓글을 달려면 로그인하십시오.

채택된 답변

Suman
Suman 2022년 11월 9일
Hi Giovanni,
I am assuming the value of "Mv" to be a scalar and the summation equation to be the following:
As per the above summation equation, the "For" loop logic that you have written doesn't match with the equation logic.
You can use the following code snippet instead of the existing "For" loop:
DeltaQ_err = 0;
for k = 1:N
T = m_i(k) * DeltaV_id(k);
DeltaQ_err = DeltaQ_err + T;
end
DeltaQ_err
I hope this information is helpful to you.
Cheers,
Suman
  댓글 수: 1
Giovanni Curiazio
Giovanni Curiazio 2022년 11월 10일
Thanks a lot. Yes Mv in scalar. This is how i did, it's almost the same as yours:
DeltaQ_err=zeros(1,3);
DeltaQ=zeros(N,3); %inizializzo matrice
for k=1:N
DeltaQ(k,:)=(m_i(k)*DeltaV_id(k,:));
DeltaQ_err=DeltaQ_err+DeltaQ(k,:);
end
DeltaQ_err

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!