Youwei Zhu
Youwei Zhu

Reputation: 79

How to return multiple value in matlab?

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

Upvotes: 1

Views: 1875

Answers (1)

Santhan Salai
Santhan Salai

Reputation: 3898

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.

Upvotes: 3

Related Questions