Error Using ode45, trying to call a function into ode45? please help

2 views (last 30 days)
clc;
clear;
m1 =250; m2=40; c1=2000; k1=15000; k2=150000;
duf =@(t,y) [y(2);
1/m1*(c1*(y(4)-y(2))+k1*(y(3)-y(1)));
y(4);
1/m2*(-c1*(y(4)-y(2))-k1*(y(3)-y(1))+k2*(@yfunction-y(3)))]
[t,y] =ode45(duf,[0,4.5 ], [0; 0;0;0]);
and I am trying to call this piecewise function
function [f] = yfunction(T)
% Define time and function for the 1st interval
t1=[0:0.0005:0.05];
f1=4.*t1;
% Define time and function for the 2nd interval
t2=[0.05:0.0005:0.3];
f2=0.2*ones(size(t2));
% Define time and function for the 3rd interval
t3=[0.3:0.0005:0.35];
f3=-4.*(t3-0.35);
T= [t1 t2 t3];
f= [f1 f2 f3];
this is the error i am recieving
Undefined operator '-' for input arguments of
type 'function_handle'.
Error in
problem2>@(t,y)[y(2);1/m1*(c1*(y(4)-y(2))+k1*(y(3)-y(1)));y(4);1/m2*(-c1*(y(4)-y(2))-k1*(y(3)-y(1))+k2*(@yfunction-y(3)))]
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I
sets args{1} to yp0.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name,
ode, tspan, y0, options, varargin);
Error in problem2 (line 10)
[t,y] =ode45(duf,[0,4.5 ], [0; 0;0;0]);

Answers (1)

Nicolas Schmit
Nicolas Schmit on 23 Oct 2017
There is an error in
duf = [... @yfunction-y(3) ... ]
You are calling @yfunction without any argument. The correct syntax might be
@yfunction(y(3))
  1 Comment
Walter Roberson
Walter Roberson on 23 Oct 2017
That would not be valid. yfunction(y(3))-y(3) would be valid (but not sure it would be correct for the case. Maybe yfunction(t)-y(3) ... or something else.)

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!