Matlab help, function handle

In summary, the conversation involves a person seeking help with their Matlab function that solves an equation using a function handle. They have encountered an error and are discussing potential solutions, including checking input arguments, using a different integration method, and ensuring the function handle for y is defined correctly.
  • #1
Jackk
4
0

Homework Statement


Hi,

I'm writing a little Matlab function that solves an equation with the help of a function handle.The equation is

x(t) = exp(-a*t)*x0 + exp(-a*t) * int(exp(a*s)*y(s), s=0..t)

I have tried it with two following functions

2. The attempt at a solution

function #1
function ode = ode1sol(a, x0, y, tol)

% if tol is not given
if nargin == 3
tol = 1e-6;
end

t = 0:1:10;

% sub3 being a subfunction
ode = sub3(a,x0,y,t,tol);

function #2
function x = sub3(a,x0,y,t,tol)

f = @(s) exp(a*s) .* y(s);

for i = 1:length(t)
x(i) = exp(-a*t(i))*x0 + exp(-a*t(i)) .* quad(f,0,t(i),tol);
end

--
when I write
t=0:1:10;
ode1sol(1,1,sin(6*t))

Matlab gives me this error
? Subscript indices must either be real positive integers or logicals.

Error in ==> sub3>@(s)exp(a*s).*y(s) at 5
f = @(s) exp(a*s) .* y(s);

Error in ==> quad at 77
y = f(x, varargin{:});

Error in ==> sub3 at 8
x(i) = exp(-a*t(i))*x0 + exp(-a*t(i)) .* quad(f,0,t(i),tol);

Error in ==> ode1sol at 13
ode = sub3(a,x0,y,t,tol);

I'm confusing the indices somehow but I don't know why...
 
Physics news on Phys.org
  • #2




Hello,

It looks like you are on the right track with your function. However, there are a few things that could be causing the error you are receiving. Here are a few suggestions to try:

1. Make sure that your input arguments are in the correct order. Based on the code provided, it looks like your function should be called as ode1sol(a, x0, y, tol).

2. Check the dimensions of your input arguments. For example, make sure that the length of t is the same as the length of y.

3. Try using a different integration method instead of quad. For example, you could try using the built-in function trapz or simpson.

4. Make sure that your function handle for y is defined correctly. You may need to use the "arrayfun" function to evaluate y at each value of t.

I hope this helps. Let me know if you are still having trouble and I can try to provide more specific suggestions. Good luck with your function!
 

1. What is Matlab?

Matlab is a powerful programming language and numerical computing environment commonly used by scientists, engineers, and researchers for data analysis, simulation, and visualization.

2. What is the purpose of the "help" function in Matlab?

The "help" function in Matlab is used to provide information and documentation about a specific function or command. It can be accessed by typing "help " in the Command Window.

3. How do I use function handles in Matlab?

Function handles in Matlab allow you to create a variable that represents a specific function or command. This allows you to pass functions as arguments to other functions, or to store them for later use. To create a function handle, use the "@" symbol followed by the function name.

4. Can I pass multiple inputs to a function handle in Matlab?

Yes, you can pass multiple inputs to a function handle in Matlab by using the syntax "@(input1, input2, ...)" followed by the function name. The inputs must be separated by commas and enclosed in parentheses.

5. How do I clear function handles in Matlab?

To clear function handles in Matlab, you can use the "clear" command followed by the name of the function handle, or you can use the "clear all" command to clear all variables and function handles from the workspace.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
811
  • Engineering and Comp Sci Homework Help
Replies
3
Views
801
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
316
  • Engineering and Comp Sci Homework Help
Replies
1
Views
866
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
934
  • Advanced Physics Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Back
Top