summation in matlab with for loop

11 ビュー (過去 30 日間)
vishnuvardhan naidu tanga
vishnuvardhan naidu tanga 2019 年 12 月 11 日
編集済み: vishnuvardhan naidu tanga 2019 年 12 月 12 日
how can i write the code for the following equation using for loop and summation. here the bold and the underlined part of the equation has to be summed up with change in the i values and the end -i will be the highest constant of n.
W = (kappa*T_in)/(kappa-1)*(((P(i+1)/P(i))^((kappa-1)/kappa))-i);

採用された回答

Jakob B. Nielsen
Jakob B. Nielsen 2019 年 12 月 11 日
Is it a running sum? In this case something like this, then the i'th entry in runningsum will be the sum of all previous iteration plus the current iteration.
n=100; %lets say you run 100 runs
runningsum=zeros(n,1);
for i=1:n
W(i) = (kappa*T_in)/(kappa-1)*(((P(i+1)/P(i))^((kappa-1)/kappa))-i);
runningsum(i)=sum(runningsum)+((P(i+1)/P(i))^((kappa-1)/kappa));
end
  5 件のコメント
Jakob B. Nielsen
Jakob B. Nielsen 2019 年 12 月 12 日
I dont have any simulink experience, but if functions work the same way as they do in 'regular matlab', then you simply do this, save it as RunningSumFunction.m on the path you work in, and call it in your simuling as W=RunningSumFunction(100); to do 100 runs, or 110 to do 110 runs or whatnot.
function W=RunningSumFunction(n)
running=zeros(n,1);
for i=1:n
running(i)=((P(i+1)/P(i))^((kappa-1)/kappa));
end
W = (kappa*T_in)/(kappa-1)*(sum(running)-n);
end
vishnuvardhan naidu tanga
vishnuvardhan naidu tanga 2019 年 12 月 12 日
編集済み: vishnuvardhan naidu tanga 2019 年 12 月 12 日
Thank you so much for the answer, but thats just a single part of my equation. the whole equation is as follows:
P=zeros(1,n+1 );
P(1) = P_in ;
CR = (P_out/P_in)^(1/n);
for i = 2:n
P(i) = CR*P(i-1 );
P(i+1) = CR*P(i );
end
T=zeros(1,n);
for i = 1:n-1
T(i) = ((P(i+1)/P_in)*(T_in^(kappa/(kappa-1))))^((kappa-1)/kappa);
T(i+1) = ((P(i+1)/P(i))*(T(i)^(kappa/(kappa-1))))^((kappa-1)/kappa);
end
running=zeros(n,1);
for i=1:n
running(i)=((P(i+1)/P(i))^((kappa-1)/kappa));
end
W = (kappa*M)/(kappa-1)*((sum(running))-n);
so this equations i am trying to solve in simulink as a matlab function. i need to define the outputs and the inputs as ports in simulink. so do you have any idea regarding this?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by