1

I want to return two array in this function(newton method). But when I run this script in matlab. It will only return the first array. How should I fix it?

    function [iter errorn]=Newton(func,dfunc,x0)
    i=1;
    solution=fzero(func,x0);
    while abs(x0-solution)>1e-06
      iter(i)=i;
      x0=x0-func(x0)./dfunc(x0);
      errorn(i)=abs(x0-solution);
      i=i+1;
    end
    end

The function is called by:

f1=@(x)x^3-2*x-5;
df1=@(x)3*x^2-2; 
[iter,y1N]=Newton(f1,df1,4)

But MATLAB returns an error: too many arguments

7
  • 1
    where is your function call? Commented Apr 7, 2015 at 7:32
  • check your function call how much output variable you assign to this function Commented Apr 7, 2015 at 7:36
  • f1=@(x)x^3-2*x-5; df1=@(x)3*x^2-2; [iter,y1N]=Newton(f1,df1,4);this is my function call but matlab has error "too many arguments" I can't figure out why@Karthick Rajan @Santhan Salai Commented Apr 7, 2015 at 7:44
  • the two output arrays are iter and y1N. what more do you want? Commented Apr 7, 2015 at 7:50
  • 1
    @YouweiZhu do you have a variable called "Newton" in the Workspace? Please try which Newton and verify that this is the path to your Newton function. Commented Apr 7, 2015 at 7:52

1 Answer 1

3

Add one more variable to the output of function call. Replace the variable name with the variable name of your problem.

[firstArray, secondArray] = Newton(func,dfunc,x0);

Check if the function is created as a separate function.m file and also it's visible to matlab (in the path).

Also refer to hbaderts comment - Same variable name as function name might override the function.

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

1 Comment

Thanks, realised that matlab won't overwrite existing variable, So I need to change variable name.

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.