Solve nonlinear equation using ODE45 function with different initial conditions

4 Ansichten (letzte 30 Tage)
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

Antworten (1)

Stephan
Stephan am 7 Dez. 2020
Bearbeitet: Stephan am 7 Dez. 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

Community Treasure Hunt

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

Start Hunting!

Translated by