Receiving "Incorrect number or types of inputs or outputs for function rhs." error.

9 views (last 30 days)
I am not incredibly familiar with Matlab yet but have a project in a non coding class where a skeleton of code was provided and essentially became fill in the blanks, however I keep recieving this error and have no idea how to fix it as it is beyond my understanding of Matlab thusfar.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Rudimentary script to solve an initial value problem consisting of the
% following system of two differential equations on the interval a<=t<=b
%
% x1'(t) = alpha*x1+beta*x2, x1(a) = x1_0
% x2'(t) = gamma*x1+delta*x2, x2(a) = x2_0
%
% Look for "*** Your entry i ***" where i=1,2,3,4,5,6 to make appropriate
% changes to solve your specific system
%
% Running the script as is outputs the unit circle as the trajectory with
% x1(t) = cos t and x2(t) = -sin t for the time series
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Housekeeping
clc ; % clear the command window
clear; % clear all variables
close all; % close all figures
% ODE45 options
options = odeset('AbsTol',1e-10,'RelTol',1e-7);
% Independent variable
a = 0; % left endpoint of t interval *** Your entry 1 ***
b = 20; % right endpoint of t interval *** Your entry 2 ***
h = 0.01; % stepsize *** Your entry 3 ***
tt = a:h:b;
% Initial value of dependent variables
x1_0 = 5; % initial value of x1 *** Your entry 4 ***
x2_0 = 1; % initial value of x2 *** Your entry 5 ***
x0 = [x1_0 x2_0];
% Solve the system
[t,soln] = ode45(@(t,x) rhs(t,x),tt,x0,options);
Warning: Failure at t=3.699620e-01. Unable to meet integration tolerances without reducing the step size below the smallest value allowed (8.881784e-16) at time t.
% Plot solutions
figure(1); % time series of x1(t)
plot(t,soln(:,1));
xlabel('$t$','Interpreter','latex','FontSize',20);
ylabel('$x_1$','Interpreter','latex','FontSize',20,'Rotation',0)
grid on
figure(2); % time series of x2(t)
plot(t,soln(:,2));
xlabel('$t$','Interpreter','latex','FontSize',20);
ylabel('$x_2$','Interpreter','latex','FontSize',20,'Rotation',0);
grid on
figure(3); % phase portrait
plot(soln(:,1),soln(:,2));
xlabel('$x_1$','Interpreter','latex','FontSize',20);
ylabel('$x_2$','Interpreter','latex','FontSize',20,'Rotation',0)
grid on
axis square
% Right hand side of the system differential equations
function [xp] = rhs(t,x) % do not change this line
% *** Your entry 6 *** (start entering your system here)
alpha = 1.5;
beta = 1.1;
gamma = 2.5;
delta = 1.4;
x1prime = -alpha*x(1) + beta*x(1)*x(2);
x2prime = gamma*(1-0.5*x(2)) + delta*x(1)*x(2);
% *** Your entry 6 *** (stop entering your system here)
xp = [x1prime ; x2prime]; % do not change this line
end % do not change this line
and this is the full error I am recieving
Incorrect number or types of inputs or outputs for function rhs.
Error in @(t,x)rhs(t,x)
Error in odearguments (line 93)
f0 = ode(t0,y0,args{:});
^^^^^^^^^^^^^^^^^^^^^^^^
Error in ode45 (line 104)
odearguments(odeIsFuncHandle,odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Answers (1)

Cris LaPierre
Cris LaPierre on 20 Feb 2025
Moved: Cris LaPierre on 20 Feb 2025
The code appears to run here without error. Try clearing your workspace and then just running the code you shared.
  4 Comments
Cris LaPierre
Cris LaPierre on 20 Feb 2025
You indicated you are using R2024b, the same as what is running here, so unlikley.
It looks like your script is calling the rhs function in the MATLAB Symbolic Toolbox rather than your function. Verify this by changing the name of your function to something like myRHS.
Is rhs a local function in your script or in a separate file? If a separate file, make sure it is either in the current folder, or in a folder that has been added to your MATLAB path.

Sign in to comment.

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!