How to plot the graphs when b2 varies from 0 to 2 in the code I attached below?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
I tried to use a for loop but getting error. Could you please tell how can I get graph for b2 = 0:2 and how to merge them on graph?
Accepted Answer
KALYAN ACHARJYA
on 2 Aug 2019
Edited: KALYAN ACHARJYA
on 2 Aug 2019
"How to plot the graphs when b2 varies from 0 to 2 in the code I attached below?"
Function file:
function v = funtryp(t,y,Z,b2);
r_v = 0.33; r_l = 0.33;b1 = 2*10^(-9);b3 = 0.5;t2 = 10;d_v = 0.5;d_l = 0.5;d_m = 0.1;c = 10^(2);
C = 10^(8);x = 2;gamma = 2.71;delta = 2.5*10^(-2);
ylag1 = Z(:,1);
v = zeros(5,1);
v(1) = r_v*y(1)-b1*y(4)*y(1)-b2*y(1)-d_v*y(5)*y(1);
v(2) = r_l*y(2)+b1*y(4)*y(1)+b2*y(1)-gamma*y(2)-d_l*y(5)*y(2);
v(3) = gamma*y(2)-delta*y(3)-d_m*y(5)*y(3);
v(4) =y(1)+y(2)-b3*y(4);
v(5) = c*(((ylag1(1)+ylag1(2)+ylag1(3))/C)^x)*(1-y(5));
end
Main Script:
options = ddeset('RelTol',1e-5,'AbsTol',1e-8,'InitialY',[20,0,0,0,0]);
b2=0:.25:2;
for i=1:length(b2);
sol = dde23('funtryp',100,[0,0,0,0,0],[0,1500],options,b2(i));
plot(sol.x,sol.y(5,:),LineWidth',2)
hold on;
end
Please note: when you assined b2=0 at initial it taking longer time, please check the issue. rest the code is ok, I have check the following one b2 ranges 1 to 2 with step size 0.25
options = ddeset('RelTol',1e-5,'AbsTol',1e-8,'InitialY',[20,0,0,0,0]);
b2=1:.25:2;
for i=1:length(b2);
sol = dde23('funtryp',100,[0,0,0,0,0],[0,1500],options,b2(i));
plot(sol.x,sol.y(5,:),'LineWidth',2)
hold on;
end
Result:

Hope you get the idea, how you can pass the different b2 values to the function file
8 Comments
KALYAN ACHARJYA
on 2 Aug 2019
Have you read my later comment?
parag gupta
on 2 Aug 2019
Hey! thanks .
How u gave different colour to the graph for different values of b2 and how to add legend ?
KALYAN ACHARJYA
on 2 Aug 2019
Edited: KALYAN ACHARJYA
on 2 Aug 2019
Welcome @Parag
See the plot line, I have removed 'g' from that line
parag gupta
on 2 Aug 2019
Edited: parag gupta
on 2 Aug 2019
Hey, I did the same for x but getting an error. (x is a parameter in my function file)
x=1:.25:2;
for i=1:length(x);
sol = dde23('funtryp',100,[0,0,0,0,0],[0,1500],options,x(i));
plot(sol.x,sol.y(5,:),'LineWidth',2)
hold on;
end
KALYAN ACHARJYA
on 2 Aug 2019
Edited: KALYAN ACHARJYA
on 2 Aug 2019
Legend:
options = ddeset('RelTol',1e-5,'AbsTol',1e-8,'InitialY',[20,0,0,0,0]);
b2=1:.25:2;
for i=1:length(b2);
sol = dde23('funtryp',100,[0,0,0,0,0],[0,1500],options,b2(i));
plot_data(i)=plot(sol.x,sol.y(5,:),'LineWidth',2)
legend_data{i}=['b2=',num2str(b2(i))];
hold on;
end
legend(plot_data,legend_data,'Location','SouthEast');

Hope it helps!
parag gupta
on 2 Aug 2019
Hey, I did the same for x instead for b2 but getting an error. (x is a parameter in my function file)
x=1: .25:2;
for i=1:length(x);
sol = dde23('funtryp',100,[0,0,0,0,0],[0,1500],options,x(i));
plot(sol.x,sol.y(5,:),'LineWidth',2)
hold on;
end
KALYAN ACHARJYA
on 2 Aug 2019
See its running in my case, exit Matlab and repeat it (copied the code from my answer), both function file and main script.
parag gupta
on 2 Aug 2019
Edited: parag gupta
on 2 Aug 2019
@kalyan thanks its running.
More Answers (0)
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)