for文の使い方
142 views (last 30 days)
Show older comments
現在for文を用いてa=1のもとbを1から100まで1づつ増やして最小にしたい関数C(a,b)が最小になるbを求めるプログラムを以下のように書いています。
a = 1
b = -10:1:10
C1= zeros(1,length(b))
for i=1:length(b)
C(i) = a*cos(b(i))+5*sin(b(i))
end
[A,B]=min(C)
これによって関数Cの最小値AとB(最小になるときのb)を求めています。
この処理をa=1から100まで繰り返し,aとBのグラフを書きたいのですがfor文の使い方がよくわからずできません。参考になるページ等あれば教えてください。
0 Comments
Accepted Answer
Atsushi Ueno
on 20 Nov 2022
> for文の使い方がよくわからずできません。参考になるページ等あれば教えてください。
> この処理をa=1から100まで繰り返し,aとBのグラフを書きたい
a = 1:1:100;
b = -10:1:10;
B = zeros(size(a)); % 変化の無い変数はループの外に出す事が望ましいです
for k = 1:length(a)
C = zeros(1,length(b)); % C1になっていたのでCに変更しました
for i = 1:length(b)
C(i) = a(k)*cos(b(i)) + 5*sin(b(i));
end
[~,B(k)] = min(C);
end
plot(a,B,'-o');
0 Comments
More Answers (0)
See Also
Categories
Find more on Simulink 環境のカスタマイズ in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!