Plotting summation function in loop

Plotting summation function in loop

4 件のコメント

KL
KL 2017 年 10 月 28 日
Did you try anything yet?
Yuhang Ma
Yuhang Ma 2017 年 10 月 28 日
編集済み: Yuhang Ma 2017 年 10 月 28 日
Yes, I did. Tried to use loop, but it doesn't work. Here are my code
lnR = 0.2*rand(1,500);
X = exp(lnR)-1.1;
g = 0;
for i = 1:100
g = g + X(i)/(1+a*X(i))
fplot(g,[0:100:5])
end
Jan
Jan 2017 年 10 月 28 日
@Yuhang Ma: Whenever you write "does not work" in a forum, add the details. It is much easier to solve a problem than to guess, what the problem is.
Yuhang Ma
Yuhang Ma 2017 年 10 月 28 日
Sorry, this is my first time asking question in a forum. Let finish my question. My code is as follow. I was trying to use for loop the alpha to find value g in function f and plot the graph. However the figure shows no line.
lnR = 0.2*rand(1,500);
X = exp(lnR)-1.1;
a = linspace(0,5,100);
f = @(a) sum(X./(1+a.*X));
for i = 1:length(a)
g = f(a(i));
end
plot(a,g)

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

 採用された回答

KL
KL 2017 年 10 月 28 日
編集済み: KL 2017 年 10 月 28 日

1 投票

Alright, you have defined lnR and X correctly,
lnR = 0.2*rand(1,500);
X = exp(lnR)-1.1;
Then they have asked you to define an inline function, I'd recommend to read this link: https://de.mathworks.com/help/matlab/matlab_prog/anonymous-functions.html
For your case,
func = @(alpha) sum(X./(1+alpha.*X));
Now you have to define alpha in the interval 0 to 5 with 100 mesh points,
hint: alpha=linspace(from,to,no_of_points)
then you have to pass alpha to func to calculate g, you will get one value of g for every alpha, so finally you will have 100 g, which you can plot using,
plot(g)

2 件のコメント

KL
KL 2017 年 10 月 28 日
replace
g = f(a(i));
with
g(i) = f(a(i));
this way you store results from every iteration in an array, previously you had been overwriting.
Yuhang Ma
Yuhang Ma 2017 年 10 月 28 日
Thank you so much!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeFunction Creation についてさらに検索

タグ

質問済み:

2017 年 10 月 28 日

編集済み:

KL
2017 年 10 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by