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.

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
Unrecognized function or variable 'A'.
[time,Y] = ode45(f,tspan,[0;0;0]);
plot(time,Y(:,3))

5 Comments

how we can do that in this program and how this can solve the problem?
Do you have the values for these four (4) parameters , , , ?
What exactly is this system? Did you formulate the problem correctly such that and equations depend on states , , , that are fed back to that subsystem?
What are the initial values for the 15 state variables at time ?
these are the equation that I want to solve by using ode45, where i = 1 to 5.
conditions A0 =0 and Ψ0 = 0 and whenever the i+1>=5 then (( i+1) - 5), means the A6 will be A1, A7 will be A2,
Ψ6 = Ψ1 and Ψ7 = Ψ2.
its like a circle i = 1 2 3 4 5 6 7
A1 A2 A3 A4 A5 A1A2....
Won't this create an Ouroboros Loop that yields no solution?

Sign in to comment.

Answers (1)

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 Comments

here you generate a vector A and O randomly, what I want to do is different.
i want to sovle dA(i)/dt and dO(i)/dt for i =1 to 5, so the there will be dA1/dt, dA2/dt,dA3/dt,dA4/dt and dA5/dt and similarly we have dO1/dt to dO5/dt there all are coupled.
then i want to solve dP(i)/dt =dO(i+1)/dt - dO(i)/dt, and then there is P1 to P5, hence there will be 15 coupled differential equation to solve, that's why I'm using the for loop.
is it possible to solve this by for loop?
===========================================================================
No. Your system cannot be solved with the information given.
You define in terms of . For i = 1 that would define in terms of which does not exist.
You define in terms of . For i = 5, that would define in terms of but does not exist.
The system cannot be resolved without defining the boundary cases for the first and last functions.
do you have any idea how can i do this ? and btw thanks for all the help.
Usually, there are special "boundary" ODEs for the cases i=1 and i=N that differ from the ODEs for 1<i<N.
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

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Asked:

on 31 Jul 2022

Commented:

on 2 Aug 2022

Community Treasure Hunt

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

Start Hunting!