Solve nonlinear equation using ODE45 function with different initial conditions
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have written a function plus a script to solve a non linear single degree of freedom pendalum system.I don't know how to solve the nonlinear equation with ODE45 function for different values of initial conditions.How could I do that?.I have used a for loop but it gives me just the state vector of the last initial condtion.I want the state vector for all initial condtions.Here are my function and the script:
function xDot = of(x,g,L,u)
xDot = zeros(2,1);
xDot(1) = x(2);
xDot(2) = ((g./L)*sin(x(1)))+u;
end
%% Main Script
clc;clear;close all;
L = 1;
g = 9.81;
h = 0.25;
t = [0:h:5];
A = [0 1;(g/L) 0];
B =[0 1]';
Ics = [pi,0;pi/2 0;pi/5 0;0.001 0;pi 0.5;pi/2 0.5;pi/5 0.5;0.001 0.5];
[Poles,~] = eig(A); %Poles Of Closed LOop System
R = 0.001;
Q = eye(2);
K = lqr(A,B,Q,R);
u = @(x)-K*(x);
for i = 1:size(Ics, 1)
[~, X] = ode45(@(t, x)of(x, g, L, u(x)), t, Ics(i, :));
end
0 commentaires
Réponses (1)
Stephan
le 7 Déc 2020
Modifié(e) : Stephan
le 7 Déc 2020
Corrected:
%% Main Script
L = 1;
g = 9.81;
h = 0.25;
t = [0 5];
A = [0 1;(g/L) 0];
B =[0 1]';
Ics = [pi,0;pi/2 0;pi/5 0;0.001 0;pi 0.5;pi/2 0.5;pi/5 0.5;0.001 0.5];
[Poles,~] = eig(A); %Poles Of Closed LOop System
R = 0.001;
Q = eye(2);
K = lqr(A,B,Q,R);
u = @(x)-K*(x);
for ii = 1:size(Ics, 1)
[t, X(:,:,ii)] = ode45(@(t, x)of(t,x, g, L, u), t, Ics(ii,:));
end
function xDot = of(~,x,g,L,u)
xDot = zeros(2,1);
xDot(1) = x(2);
xDot(2) = ((g./L)*sin(x(1)))+u(x);
end
X is saved as a 3D array - To show the results for the single IC's use:
>> plot(t,X(:,:,1)) % first set of IC
>> plot(t,X(:,:,2)) % second set of IC
0 commentaires
Voir également
Catégories
En savoir plus sur Ordinary Differential Equations dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!