how do I pre-allocate the program. It highlights it in red and I am not able to do that. Thanks for helping.

1 回表示 (過去 30 日間)
Dereje
Dereje 2017 年 11 月 30 日
コメント済み: Greg 2017 年 11 月 30 日
for i=1:numel(rho)-1
rho0=17.1;
zspan = [z(i) z(i+1)];
NS =(rho(i+1)-rho(i))/(z(i+1)-z(i));
[t,v] = ode45(@(t,v)rhs(t,v,NS), zspan, v0);
v0 = [v(end,1) ; v(end,2) ; v(end,3)];
zsol = [zsol;t];
v1sol = [v1sol;v(:,1)];
v2sol = [v2sol;v(:,2)];
v3sol = [v3sol;v(:,3)];
end
  1 件のコメント
Greg
Greg 2017 年 11 月 30 日
Please format your question so we can read it as code. Use the "{} Code" button if you need help with formatting syntax.

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

回答 (3 件)

Guillaume
Guillaume 2017 年 11 月 30 日
Assuming your ODE returns a 3x3 array:
v1sol = zeros(3, numel(rho) - 1);
v2sol = zeros(3, numel(rho) - 1);
v3sol = zeros(3, numel(rho) - 1);
zsol = zeros(numel(rho) - 1, 1);
rho0=17.1;
for i=1:numel(rho)-1
zspan = [z(i) z(i+1)];
NS =(rho(i+1)-rho(i))/(z(i+1)-z(i));
[t,v] = ode45(@(t,v)rhs(t,v,NS), zspan, v0);
v0 = [v(end,1) ; v(end,2) ; v(end,3)];
zsol(i) = t;
v1sol(:, i) = v(:, 1);
v2sol(:, i) = v(:, 2);
v3sol(:, i) = v(:, 3);
end
  1 件のコメント
Greg
Greg 2017 年 11 月 30 日
I would go ahead and use
nrho = numel(rho) - 1;
Just a little cleaner in my opinion. Nothing to do with pre-allocation.

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


Dereje
Dereje 2017 年 11 月 30 日
@Guillame there is an error(Matrix mis-match) on
sol(i) = t;
@Greg I don't understand your point.
  1 件のコメント
Greg
Greg 2017 年 11 月 30 日
Please use the "Comment on this Question" and "Comment on this Answer" links instead of creating a new answer. I know the web form is confusing the first time.
You can also edit your original question to attach the code. This makes it easier for others (people may have the same question you have) to find.

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


Dereje
Dereje 2017 年 11 月 30 日
I have attached my original codes.

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by