How to run a loop for multiple variations of a variable.

3 ビュー (過去 30 日間)
Westin Messer
Westin Messer 2018 年 10 月 22 日
編集済み: Matt J 2018 年 10 月 22 日
Hello, I'm new to MATLAB and coding and I'm trying to vary the parameter 'p' from 0 to 3 in incriminates of 1 inside the loop which should give me three different sets of data for the v_d equation but I can't get it to work. Can anyone see what I am doing wrong? Thanks!
e=0.01; k=4; a=0.1; p = 0:1:3;
i = 0.001;
u=zeros(100000,1);
v=zeros(100000,1);
v_d=zeros(100000,1);
t=zeros(100000,1);
% Initial conditions:
u(1)=0.6;
v(1)=0.0;
v_d(1)=0.0;
t(1)=0;
dt=0.001;
for i=1:1:50000
t(i+1)=t(i)+dt;
u(i+1) = u(i)+ dt*((1/e)*((k*u(i)*(u(i)-a)*(1-u(i)))-v(i)));
v(i+1) = v(i)+ dt*(u(i)-v(i));
v_d(i+1) = v_d(i)+ dt*(u(i)-p*v_d(i));
end
  1 件のコメント
Matt J
Matt J 2018 年 10 月 22 日
which should give me three different sets of data for the v_d equation
Don't you mean 4 different sets: 0,1,2,3

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

採用された回答

Matt J
Matt J 2018 年 10 月 22 日
v_d=zeros(50000,4); %<--change
t(1)=0;
dt=0.001;
for i=1:1:50000
t(i+1)=t(i)+dt;
u(i+1) = u(i)+ dt*((1/e)*((k*u(i)*(u(i)-a)*(1-u(i)))-v(i)));
v(i+1) = v(i)+ dt*(u(i)-v(i));
v_d(i+1,:) = v_d(i,:)+ dt*(u(i)-p.*v_d(i,:)); %<--change
end
  2 件のコメント
Westin Messer
Westin Messer 2018 年 10 月 22 日
Thanks! In another part of my code I have:
p = 0:1:3;
upts=(-2:.05:2);
vnullpts_d=upts/p;
And I get the following error:
Matrix dimensions must agree.
Error in BME_721_Midterm (line 50)
vnullpts_d=upts/p;
Do you know how I'd fix this to get four different values of vnullpts?
Matt J
Matt J 2018 年 10 月 22 日
編集済み: Matt J 2018 年 10 月 22 日
4 different values per value of upts, you mean? So, you want the result as a 4x81 matrix? If so, one option is
vnullpts_d=upts./p(:);
Although, be mindful that you have a divide-by-zero condition, because of p(1)=0.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by