0

I have this very simple function in matlab.

function [f]=f1(a, xx)
xx
f = -exp(-a(1)*(xx(1)-1)^2 - a(2)*(xx(2)-1)^2) - exp(-a(1)*(xx(1)+1)^2 -a(2)*(xx(2)+1)^2);

It does print the value of xx, and then complains "Input argument "xx" is undefined". How is this possible? What's going on?

I call the function with feval Here is another example that doesn't use feval. I get the error Input argument "xxx" is undefined. Please help me, I have no idea what's going on and I'm stuck.

I have: function [ ans ] = f2( xxx ) xxx %f2 is 1/(1+xxx^2), the function for problem 2 ans = 1 / (1+xxx^2); end

one file is neville.m

Q = neville(x,xi,f2) %NEVILLE implements Neville method for polynomial interpolation

nplus1 = max(size(xi)); Q = zeros(nplus1); Q(:,1) =f2(xi);

for i = 2 : nplus1 for j = 2 : i Q(i, j) = ((x - xi(i-j)) * Q(i, j-1) - (x - x(i))*Q(i-1, j-1)) / (x(i) - x(i-j)); end; end;

The other file is neville_driver.m N = 6; aux = [0:6]; xi = -5 + 10*aux/N;

Q = neville(4, xi, f2)

1
  • 1
    how are you calling your function? Commented Mar 21, 2011 at 2:02

2 Answers 2

2

Looks like you're passing a function f2 into neville.m. Try using Q=neville(x,xi,@f2); Also, if xi is a vector, you should use element wise multiplication (and raising it to the nth power) using a dot (.) before the operation, else it will give an error. i.e., 1/(1+xxx.^2).

Sign up to request clarification or add additional context in comments.

4 Comments

So, the idea is that in neville_driver.m I want to use f2 as fi, but in neville.m I want to have a function at argument. How exactly do I do this?
fi is a function parameter. In this particular situation, I am passing f2 in the place of fi.
I also get this error message:Attempt to execute SCRIPT neville as a function. What is going on???
it means that you have not defined neville as a function. at the moment, it's just a plain matlab script, which you're probably trying to execute as a function either by passing variables or by using a function handle. this will result in an error. you should rewrite neville as a function.
0

Is xx a function? Because the syntax xx(2) is a function call. Did you mean xx*2 ?

Edit: your first example code works fine for me:

feval(@f1,[2,2],[10,10]);

Prints xx and then the result.

Edit2: and the code for f1:

function [f]=f1(a, xx)
xx
f = -exp(-a(1)*(xx(1)-1)^2-a(2)*(xx(2)-1)^2)-exp(-a(1)*(xx(1)+1)^2-a(2)*(xx(2)+1)^2);
end

2 Comments

Perhaps I should use handle when I use f2 as function argument?
As far as I know, it's not necessary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.