? Input argument y is undefined.

  • Thread starter Thread starter okkadu
  • Start date Start date
  • Tags Tags
    Argument Input
Click For Summary
SUMMARY

The error "Input argument 'y' is undefined" occurs in MATLAB when using the ode45 function with an incorrectly defined function handle. To resolve this, users must define the function in an .m file and call it using a function handle (e.g., @sampleode) rather than a string. The discussion suggests two solutions: saving the function in a separate file or using nested functions within a single file. Both methods ensure that the function is properly recognized by ode45, allowing for successful execution of the differential equations.

PREREQUISITES
  • Familiarity with MATLAB syntax and function definitions.
  • Understanding of the ode45 function for solving ordinary differential equations.
  • Knowledge of function handles in MATLAB.
  • Basic concepts of local and global variables in programming.
NEXT STEPS
  • Learn about MATLAB function handles and their usage.
  • Explore the differences between local and global variables in MATLAB.
  • Study the implementation of nested functions in MATLAB.
  • Practice using ode45 with various differential equations to gain proficiency.
USEFUL FOR

MATLAB beginners, students learning numerical methods, and developers working with differential equations who need to troubleshoot function-related errors in MATLAB.

okkadu
Messages
1
Reaction score
0
? Input argument "y" is undefined.

HII Friends
Iam little bit new to MATLAB and iam trying to use ode45 and wrote code as

function dy = sampleode(t,y)

dy=zeros(2,1);

dy(1)=-0.192*y(1)*log(y(1)/y(2).^3);

dy(2)=(1/3)*(5.85-0.00873*y(2).^2-0.15*0*y(2)-0.02*y(2));

[t,y]=ode45('sampleode', [0:5:40],[1;1]);


BUt iam getting the below error:

? Input argument "y" is undefined.

Error in ==> sampleode at 3
dy(1)=-0.192*y(1)*log(y(1)/y(2).^3);
I saved my mfile as sampleode.m .please suggest me any suggestions. Thanks in advance
 
Physics news on Phys.org
Hi,

I'm also a newbie in MATLAB, but two things strike me as odd here
1) In the following line you multiply by 0. Did you really mean to do that?
dy(2)=(1/3)*(5.85-0.00873*y(2).^2-0.15*0*y(2)-0.02*y(2));
2) I was under the impression ode45 required a function handle not a string containing the name of the function (though I may be wrong there)
Moreover, what are you inputing as your y as you didn't give the command you use to run the function.
Regardless, sorry I can't be much help.
 
Matlab is strange when working with functions.

One solution is to define the function (and save it in an .m file) and then running ode45 in console. The other (wich i prefer) is to work with nested functions.

In the first one, you should do a sampleode.m file with the following code

Code:
function dy = sampleode(t,y)
  dy=zeros(2,1);
  dy(1)=-0.192*y(1)*log(y(1)/y(2).^3);
  dy(2)=(1/3)*(5.85-0.00873*y(2).^2-0.15*0*y(2)-0.02*y(2));
end

and then in console type

Code:
[t,y]=ode45(@sampleode, [0:5:40],[1;1]);

(nothe the @).

The second solution works better when you are using a more sophisticated code and would be using nested functions. Create a file named ord.m with the following code:

Code:
function ord

[t,y]=ode45(@sampleode, [0:5:40],[1;1]);

  function dy = sampleode(t,y)
    dy=zeros(2,1);
    dy(1)=-0.192*y(1)*log(y(1)/y(2).^3);
    dy(2)=(1/3)*(5.85-0.00873*y(2).^2-0.15*0*y(2)-0.02*y(2));
  end

end

That way, when you type
Code:
ord
in console, Matlab will perform ode45 on the function sampleode. If you add the line
Code:
plot(y,U);
after ode45 but before defining sampleode, then Matlab will perform ode45 and plot the solution every time you type
Code:
ord
in console.

You should check the diference between local and global variables, it could provide another way of solving your problem.
 
Last edited:

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
Replies
1
Views
18K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
6K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
Replies
4
Views
17K