there are two coupled differential equation, I try to solve that differential for the i = 1 to 5, but i think my for loop is incorrect.
1 次查看(过去 30 天)
显示 更早的评论
these are the differential equation I want to solve where the i = 1 to 5
I want to solve these differential equation but maybe the loop isn't correct or we can't apply loop here?
clc
ti = 0;
tf = 10E-6;
tspan = [ti tf];
a = 0.6;
n = 0.05;
tc = 10E-9;
r = 1.5;
F = 1;
k =1:1:5;
f = zeros(length(k),1) ;
for i = 1:length(k)
y(1) = A(i)
y(2) = O(i)
f = @(t,y) [
(-1/(2*tc)).*(1 - r/(1+ (A(i)^2)./F)).*A(i) + (n/(2*tc)).*((cos(O(i+1)- O(i))).*A(i+1) + (cos(O(i+1)- O(i))).*A(i-1)) ;
(a./(2*tc)).*(r/(1 + (A(i)^2)./F)) + (n/(2*tc)).*((A(i+1)./A(i)).*sin(O(i+1) - O(i)) - (A(i-1)./A(i)).*sin(O(i-1) - O(i)));
O(i+1) - O(i)
];
end
[time,Y] = ode45(f,tspan,[0;0;0]);
plot(time,Y(:,3))
5 个评论
回答(1 个)
Walter Roberson
2022-7-31
Note that because you use A(i+1) and O(i-1) you cannot produce plots for the first or last entries in A or O.
%number of data points
N = 5;
%put in your real A and O data here!!
A = randn(1,N);
O = rand(1,N) * 10;
%process data
ti = 0;
tf = 10E-6;
tspan = [ti tf];
a = 0.6;
n = 0.05;
tc = 10E-9;
r = 1.5;
F = 1;
k = 1:N;
f = zeros(N,1) ;
for i = 2:N-1
f = @(t,y) [
(-1/(2*tc)).*(1 - r/(1+ (A(i)^2)./F)).*A(i) + (n/(2*tc)).*((cos(O(i+1)- O(i))).*A(i+1) + (cos(O(i+1)- O(i))).*A(i-1)) ;
(a./(2*tc)).*(r/(1 + (A(i)^2)./F)) + (n/(2*tc)).*((A(i+1)./A(i)).*sin(O(i+1) - O(i)) - (A(i-1)./A(i)).*sin(O(i-1) - O(i)));
O(i+1) - O(i)
];
[time,Y] = ode45(f,tspan,[0;0;0]);
plot(time, Y(:,3), 'DisplayName', "i = " + i);
hold on
end
legend show
5 个评论
Torsten
2022-7-31
Usually, there are special "boundary" ODEs for the cases i=1 and i=N that differ from the ODEs for 1<i<N.
Walter Roberson
2022-7-31
Note that once you have the definitions for the boundary ode, my recommendation would be to use the symbolic toolbox to set up the entire system of 15 coupled equations at the same time. Then follow the first example in the document for odeFunction() to convert to something that can be used with ode45
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Function Creation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!