store values from a for loop in a column vector?

I have this function and have to store the values of b(x) over the interval
x=0.01:0.01:5
b0=10;
K=2;
m=3;
for x=0.01:0.01:5
b= b0.*(x.^(m)/(K.^(m)+ x.^(m)));
end
I don't know how to store every value in a column vector, only the value of b(5). any ideas on how to correct this code?

 採用された回答

James Tursa
James Tursa 2017 年 2 月 10 日
編集済み: James Tursa 2017 年 2 月 10 日

2 投票

No need for the for-loop, just a one-liner:
b = b0.*(x.^(m)./(K.^(m)+ x.^(m))); % <-- changed the / to a ./

5 件のコメント

Margaret Winding
Margaret Winding 2017 年 2 月 10 日
It worked, thank you! But after that I am required to use the function fminbnd to find the max of the function b on the interval [0,5], but it won't let me as it says b isn't being saved as function. Do you know how to resolve that? Thank you for all your help!
James Tursa
James Tursa 2017 年 2 月 10 日
Please show the code you are using for fminbnd.
Margaret Winding
Margaret Winding 2017 年 2 月 11 日
[xmax, ymax]= fminbnd(-b, 0, 5)
James Tursa
James Tursa 2017 年 2 月 11 日
The first argument of fminbnd should be a function handle so that fminbnd can evaluate the function internally to find the solution. So you could use something like this:
b = @(x)b0.*(x.^(m)./(K.^(m)+ x.^(m))); % <-- create the function handle
[xmax, ymax]= fminbnd(b, 0, 5); % <-- call fminbnd
Or if you really wanted -b make that part of the function handle:
b = @(x)-b0.*(x.^(m)./(K.^(m)+ x.^(m))); % <-- create the function
Margaret Winding
Margaret Winding 2017 年 2 月 11 日
thank you so much for your help!

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

その他の回答 (2 件)

Jan
Jan 2017 年 2 月 12 日
編集済み: Jan 2017 年 2 月 12 日

2 投票

James' answer solves the problem efficiently. But if you or anybody else requires a loop, the pre-allocation is important:
x = 0.01:0.01:5
b0 = 10;
K = 2;
m = 3;
b = zeros(size(x)); % Pre-allocate!
for ix = 1:numel(x) % Use index according to vector x
b(ix) = b0 * (x(ix) ^ m / (K ^ m + x(ix) ^ m));
end
Letting an array grow iteratively wastes a lot of resources: In the first iteration a scalar double is reserved and assigned. In the second iteration, a vector of two doubles is reserved, the former contents is copied and the last value is assigned. If you have 1000 elements, Matlab has to reserve sum(1:1000) elements by this way, which are 500'500 and copy almost the same number of doubles. For 1 million elements, we are talking about 2TB of data already, although the final result occupies 8MB RAM only (8 byte per double).
John BG
John BG 2017 年 2 月 10 日
編集済み: John BG 2017 年 2 月 10 日

0 投票

may be you want to keep the for loop because there may be more omitted lines in the loop
x=0.01:0.01:5
b0=10;
K=2;
m=3;
for x=0.01:0.01:5
b= [b;b0.*(x.^(m)/(K.^(m)+ x.^(m)))];
end

カテゴリ

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

質問済み:

2017 年 2 月 10 日

編集済み:

Jan
2017 年 2 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by