How to solve loop equation with given data

Dear all,
I want to solve the equation below.
first column of excel sheet is 'a' and 2nd column is 'b' coresponding to 'a'. For each a, there is m in the interval 0 to 3 with increment of 0.01 (m=0:0.01:3). How to solve ?

6 件のコメント

Adam Danz
Adam Danz 2019 年 3 月 12 日
"I want to solve the equation below."
What have you tried so far?
Manish Kumar
Manish Kumar 2019 年 3 月 13 日
I have tried
m = 0:0.01:3
for i=1:421
eff(i,:) = ((0.65.*(a(i)-m-0.3).*b(i)))./10
end
KSSV
KSSV 2019 年 3 月 13 日
What is your problem? You got it right? Do intialze the variable in loop: beofre loop begins
eff = zeros(421,301) ;
Adam Danz
Adam Danz 2019 年 3 月 13 日
Yeah, this looks right. Just initialize 'eff' as KSSV recommended. Any other questions?
Manish Kumar
Manish Kumar 2019 年 3 月 13 日
I have tried
m = 0:0.01:1
eff = zeros(421,301)
for i=1:421
eff(i,:) = ((0.65.*(a(i)-m-0.3).*b(i)))./10 ;
end
xlswrite ('1.xlsx',[a(:),m(:),eff(:)]);
error is coming:
Unable to perform assignment because the indices on the left side are not
compatible with the size of the right side.
Adam Danz
Adam Danz 2019 年 3 月 13 日
The reason it stopped working is because you changed the size of 'm' but didn't change the size of 'eff'. I added a solution below that corrects this and allows you to use any size of 'm' without needing to change the size of 'eff'.

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

回答 (1 件)

Adam Danz
Adam Danz 2019 年 3 月 13 日
編集済み: Adam Danz 2019 年 3 月 25 日

0 投票

(continuing from the comments under the question to that the question is answered).
The more responsible way to initialize the loop variable is by using variables rather than hard-coding the variable size.
a = 1:1000;
b = 1:1000;
m = 0:0.01:3
n = 421; %number of loops
eff = zeros(n,length(m)) %here we use 'n' and the size of 'm' to define 'eff'
for i=1:n
eff(i,:) = ((0.65.*(a(i)-m-0.3).*b(i)))./10 ;
end

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2019 年 3 月 12 日

編集済み:

2019 年 3 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by