필터 지우기
필터 지우기

Call a function with multiple nonlinear eqns with ODE45

조회 수: 2 (최근 30일)
Anthony Sirico
Anthony Sirico 2021년 3월 26일
편집: Star Strider 2021년 3월 27일
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.

채택된 답변

Star Strider
Star Strider 2021년 3월 27일
There were a number of errors that I corrected.
This runs and produces what appears to be appropriate output:
tspan = [0 150];
x0 = [1;0;0;0];
[t,y] = ode45(@(t,x) fnonlinear(t,x),tspan,x0);
figure
plot(t,y)
grid
xlim([0 8]) % Optional
function DX = fnonlinear(t,x)
[ap,bp,am,bm,r,gamma] = problem_parameters(t);
thetah1 = bm/bp;
thetah2 = (am-ap)/bp;
u = @(x) thetah1*r+thetah2*x;
dx1 = ap*x(1)+bp*u(x(1));
dx2 = am*x(2)+bm*r;
dx3 = -gamma*(x(1)-x(2))*r;
dx4 = -gamma*(x(1)-x(2))*x(1);
DX = [dx1;dx2;dx3;dx4];
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
.
  댓글 수: 6
Anthony Sirico
Anthony Sirico 2021년 3월 27일
What about
dx1 = ap*x(1)+bp*u(ap,x(1)) ?
Star Strider
Star Strider 2021년 3월 27일
편집: Star Strider 2021년 3월 27일
If that does what you want, code it accordingly, and add another argument to ‘u’, and create ‘u’ as a function of both arguments.
Note that ‘u’ as originally written is only a function of ‘x’ (since ‘r’ already exists in the workspace, ‘u’ inherits that value and an explicit argument for it is not necessary), so passing ‘u’ an additional argument without changing its internal coding will likely not change its result.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by