How to use a math function as an input in a written function?

I have written a function and am trying to call it using x*sin(x) as an input variable. Of course x must be defined so I use @(x) x*sin(x) but this triggers an error Unexpected matlab operator. How do I use this as an input variable correctly?
important things:
function[x,u]=finite_difference_laplace(func,a,b,N,u_a,u_b)
h=(b-a)/(N-1);
x=a:h:b;
A=zeros(N-2,N-2);
A(1,1)=-2;A(1,2)=1;A(N-2,N-2)=-2;A(N-2,N-3)=1;
for i=2:1:N-3
A(i,i-1)=1;
A(i,i+1)=1;
A(i,i)=-2;
end
b=h.^2*[func(x(2))-u_a,func(x(3:N-2)),func(x(N-1))-u_b];
uu=A\b;
u=[u_a;uu;u_b];
>> finite_difference_laplace(@(x) x*sin(x),0,10,7,0,1)
|
Error: Unexpected MATLAB operator.

5 Comments

That style of syntax seems to work fine for me. I always create the function handle as a variable and pass that in, but this way worked when i tried it just now too. Obviously I don't have your particular function though so something there might be causing the problem.
I just tried calling an arbitrary function of mine with @(x) x*sin(x) as the first argument and the failure I got was predictably at the validateattributes line at the top of my function which recognised that argument as being of type function_handle.
Ok, I edited the question so my whole code is included. I have yet to check it because I can't run it obviously, but I still don't see why not.
Strange. I just pasted your function into an m file and ran it and it ran fine up until the:
b=h.^2*[func(x(2))-u_a,func(x(3:N-2)),func(x(N-1))-u_b];
line which has matrix dimension issues, but I know you haven't been able to check that yet anyway.
I assume the function is on your path and other obvious things like that?
I'm not actually sure. Usually, when I try to run a file a window pops up and I click Add to path and that's that. How can I check that it's on path?
which finite_difference_laplace
will tell you if it is on your path (if it isn't it will say not found), but I think you would get a different error message anyway if the function is not on your path.
If you go to 'Set Path' (I can't remember where it is in R2012a - does that version have the ribbon stuff at the top?) you can see your full path and click 'Save' to ensure that something you add to your path in one session remains on your path in the next session. I would advise using the 'Move to Bottom' option on your added folders, but that is just my preference as I have had some odd conflicts caused by having my own folders above the Matlab main folders in the past.

Answers (2)

Just to be sure: your MATLAB version is not R12 or R13 from before 2003? In R14 anonymous functions were introduced ...
Titus

4 Comments

2012a is recent enough to have function handles like this, but the unexpected operator message indicates an older release to be the most likely cause.
I would have said that the error message was most likely to indicate user error (most likely to be a typo.)
Ok guys, I'm not sure what happened but it is working now. I went to my prof. and he defined func=@(x) x*sin(x), then used func as an input. But I said that seemed like an alternative (not a solution), and he said you could also just use @(x) x*sin(x), and he typed it in and it worked (aside from the dimensional errors in h that I fixed later). But I swear that was what I was doing beforehand. I used clear and ran it again just in case and it still worked.
>> finite_difference_laplace(@(x) x*sin(x),0,10,7,0,1)
ans =
0 1.6667 3.3333 5.0000 6.6667 8.3333 10.0000

This question is closed.

Tags

Asked:

on 23 Sep 2014

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!