Call a function with multiple nonlinear eqns with ODE45
显示 更早的评论
I created a function with multiple nonlinear equations, and inside that function i call on another function that uses a time parameter. In the script, I use ode45 to simulate the equations, but I get the following error:
Error using odearguments (line 95)
@(T,X)FNONLINEAR(TSPAN) returns a vector of length 1, but the length of initial conditions vector is 4. The vector returned by @(T,X)FNONLINEAR(TSPAN) and the initial conditions vector must have the same number of elements.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
I use the following code:
close all; clear; clc
tspan = [0 150];
x0 = [1;0;0;0];
[t,y] = ode45(@(t,x) fnonlinear(tspan),tspan,x0)
plot(t,y)
function DX = fnonlinear(t)
[ap,bp,am,bm,r,gamma] = problem_parameters(t);
thetah1 = bm/bp;
thetah2 = (am-ap)/bp;
u = @(t,x) thetah1*r+thetah2*x;
dx1 = @(t,x1) ap*x1+bp*u(t,x);
dx2 = @(t,x2) am*x2+bm*r;
dx3 = @(x1,x2) -gamma*(x1-x2)*r;
dx4 = @(x1,x2) -gamma*(x1-x2)*x1;
DX = @(t,x) [dx1(t,x);dx2(t,x);dx3(t,x);dx4(x1,x2)];
end
function [ap,bp,am,bm,r,gamma] = problem_parameters(t)
% problem parameters
bp = 2; am = -1; bm = 1; r = 0.5; gamma = 2;
% simulate a fault at 100 s
if t >= 100
ap = 2;
else
ap = 1; % fault causes reduced value of ap
end
end
Please let me know if i need to clarify anything.
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!