I want to write a script that reads an input text file that specifies the parameters and then uses them to solve an integral
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Gideon Sarpong
il 14 Dic 2022
Risposto: Fabio Freschi
il 16 Dic 2022
I want to write a script that reads an input text file that specifies the parameters:
a:1
b:2
c:3
d:4
x0:1
y0:1
tf:25.
Then integrate a system of equations given the parameters read from the input text file. Sytem should be integrated from t=0 to t=tf. After plot x(t) and y(t) in a single graph.
This is what I did. It gives me errors. Kindly tell me what i am doing wrong and how to solve the question. Thanks.
[q,w] = readvars('variables.txt');
a = w(1);
b = w(2);
c = w(3);
d = w(4);
x0 = w(5);
y0 = w(6);
tf = w(7);
t = 0;
x = linspace(t,tf,25);
fx = @(x,y) a*x-b*x*y;
fy = @(y,x) c*x*y-d*y;
x = linspace(t,tf,25);
for i = 1:length(x)
fx(i)= integral(@(x)(fx(x,y)),t,x(i));
end
y = linspace(t,tf,25);
for k = 1:length(y)
fy(k)= integral(@(y)(fy(y,x)),t,y(k));
end
figure (1)
plot(fx)
plot(fy)
3 Commenti
Torsten
il 14 Dic 2022
I can only repeat: you can't use "integral" to solve differential equations that depend in the dependent variable.
You must use one of the ode integrators or try "dsolve".
Risposta accettata
Fabio Freschi
il 16 Dic 2022
As suggested by @Torsten your problem is a system of first order ODEs and you must use a ODE integrator. Try this
clear variables, close all
% your params (you can instead load here your file)
a = 1;
b = 2;
c = 3;
d = 4;
x0 = 1;
y0 = 1;
tf = 25;
% define the system of ODE as anonymous function.
% The vector variable is here X, with X(1) = x, X(2) = y
odeFun = @(t,X)[a*X(1)-b*X(1)*X(2); c*X(1)*X(2)-d*X(2)];
% initial value
X0 = [x0; y0];
% time interval
tSpan = [0 tf];
% solution with ODE45
[t,X] = ode45(odeFun,tSpan,X0);
figure
plot(t,X)
xlabel('time');
legend('x','y')
0 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Ordinary Differential Equations in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!