can I apply a for loop inside the function so that I can make my program short?
Show older comments
ti = 0;
tf = 10E-6;
tspan = [ti tf];
a = 0.6;
n = 0.05;
tc = 10E-9;
r = 1.5;
F = 10;
f = @(t,y) [
(-1/(2*tc)).*(1 - r/(1+ (y(1)^2)./F)).*y(1) + (n/(2*tc)).*((cos(y(6))).*y(2)) ;
(-1/(2*tc)).*(1 - r/(1+ (y(2)^2)./F)).*y(2) + (n/(2*tc)).*((cos(y(7))).*y(3) + (cos(y(6))).*y(1));
(-1/(2*tc)).*(1 - r/(1+ (y(3)^2)./F)).*y(3) + (n/(2*tc)).*((cos(y(8))).*y(4) + (cos(y(7))).*y(2));
(-1/(2*tc)).*(1 - r/(1+ (y(4)^2)./F)).*y(4) + (n/(2*tc)).*((cos(y(10))).*y(5) + (cos(y(8))).*y(8));
(-1/(2*tc)).*(1 - r/(1+ (y(5)^2)./F)).*y(5) + (n/(2*tc)).*((cos(y(9))).*y(4));
(a./(2*tc)).*( r/(1 + (y(2)^2)./F) - r/(1 + (y(1)^2)./F) ) + (n/(2*tc)).*(((y(3)./y(2)).*sin(y(7))) - (y(2)./y(1) + y(1)./y(2) ).*sin(y(6)) );
(a./(2*tc)).*( r/(1 + (y(3)^2)./F) - r/(1 + (y(2)^2)./F) ) + (n/(2*tc)).*(((y(4)./y(3)).*sin(y(8))) - (y(3)./y(2) + y(2)./y(3) ).*sin(y(7)) + (y(1)./y(2)).*sin(y(6)));
(a./(2*tc)).*( r/(1 + (y(4)^2)./F) - r/(1 + (y(3)^2)./F) ) + (n/(2*tc)).*(((y(5)./y(4)).*sin(y(8))) - (y(4)./y(3) + y(3)./y(4) ).*sin(y(8)) + (y(2)./y(3)).*sin(y(6)));
(a./(2*tc)).*( r/(1 + (y(5)^2)./F) - r/(1 + (y(4)^2)./F) ) + (n/(2*tc)).*(((y(3)./y(4)).*sin(y(8))) - (y(5)./y(4) + y(4)./y(5) ).*sin(y(9)));
(a./(2*tc)).*( - r/(1 + (y(5)^2)./F) ) + (n/(2*tc)).*((y(4)./y(5)).*sin(y(9)));
];
can I apply here the "for loop" inside where the function define. so the program will get short.
I wrote 10 equation in this, what if I want 20 equation ? can I apply for loop in this or any method.
[time,Y] = ode45(f,tspan,[1;1;1;1;1;1;1;1;1;1].*10E-6);
plot(time,Y(:,6))
hold on
plot(time,Y(:,7))
plot(time,Y(:,8))
plot(time,Y(:,9))
hold off
4 Comments
per isakson
on 3 Aug 2022
Edited: per isakson
on 3 Aug 2022
Moved. Posted as an answer by mistake
"what if I want 20 equation" How do these twenty equations differ?
per isakson
on 3 Aug 2022
Edited: per isakson
on 3 Aug 2022
Moved

if i =20 the A0 = 0 and A21 =0 and O0 = 0 and O21 = 0 these are the condition, how can I make this short ?
per isakson
on 3 Aug 2022
Now I think I understand your question.
Are there typos in your anonymous function? This looks suspect.
e.g. "y(8)" in three rows.
e.g. "y(8)" in three rows.Does your anonymous function agree with the equations in your comment?
For more details (especially how A_0, phi_0, A_N+1 and phi_N+1 are defined), see
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!