Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Funtion output

Subject: Funtion output

From: Ryan

Date: 31 Aug, 2013 04:29:18

Message: 1 of 5

Hello,

I have what I feel like is a simple question that for some reason I cannot find the answer too. I have a very simple function, which I will post below, and for some reason it will not print out all the variables when it is finished. In other words in the first line of the function I specify I want two outputs x and f. However, when I run it MATLAB only gives me x. Any help would be appreciated.

   function [x, f] = newtonmethod(fun,dfun,x0, nmax)
    
     for m =1:nmax
        
        fx = feval(fun,x0);
        dx = feval(dfun,x0);
        d = fx/dx;
        x= x0-d;
        f= feval(fun,x);
        %fprintf('x = %f f(x) = %f\n', x, f);
        x0 = x;
    end
    
    return

Thanks,
Ryan

Subject: Funtion output

From: TideMan

Date: 31 Aug, 2013 06:02:36

Message: 2 of 5

On Saturday, August 31, 2013 4:29:18 PM UTC+12, Ryan wrote:
> Hello,
>
>
>
> I have what I feel like is a simple question that for some reason I cannot find the answer too. I have a very simple function, which I will post below, and for some reason it will not print out all the variables when it is finished. In other words in the first line of the function I specify I want two outputs x and f. However, when I run it MATLAB only gives me x. Any help would be appreciated.
>
>
>
> function [x, f] = newtonmethod(fun,dfun,x0, nmax)
>
>
>
> for m =1:nmax
>
>
>
> fx = feval(fun,x0);
>
> dx = feval(dfun,x0);
>
> d = fx/dx;
>
> x= x0-d;
>
> f= feval(fun,x);
>
> %fprintf('x = %f f(x) = %f\n', x, f);
>
> x0 = x;
>
> end
>
>
>
> return
>
>
>
> Thanks,
>
> Ryan

Show us the line where you call the function.
I suspect this is the problem.

BTW, you should be checking whether the iteration has converged and if so, break out of the loop:
if abs(d) < tol,break,end
where tol is a number like 1e-6.
Also, you should take the f= line outside the loop.
  

Subject: Funtion output

From: Ryan

Date: 31 Aug, 2013 19:24:10

Message: 3 of 5

TideMan <mulgor@gmail.com> wrote in message <3333a2f1-1c30-41cb-8824-746cd2aab7d6@googlegroups.com>...
> On Saturday, August 31, 2013 4:29:18 PM UTC+12, Ryan wrote:
> > Hello,
> >
> >
> >
> > I have what I feel like is a simple question that for some reason I cannot find the answer too. I have a very simple function, which I will post below, and for some reason it will not print out all the variables when it is finished. In other words in the first line of the function I specify I want two outputs x and f. However, when I run it MATLAB only gives me x. Any help would be appreciated.
> >
> >
> >
> > function [x, f] = newtonmethod(fun,dfun,x0, nmax)
> >
> >
> >
> > for m =1:nmax
> >
> >
> >
> > fx = feval(fun,x0);
> >
> > dx = feval(dfun,x0);
> >
> > d = fx/dx;
> >
> > x= x0-d;
> >
> > f= feval(fun,x);
> >
> > %fprintf('x = %f f(x) = %f\n', x, f);
> >
> > x0 = x;
> >
> > end
> >
> >
> >
> > return
> >
> >
> >
> > Thanks,
> >
> > Ryan
>
> Show us the line where you call the function.
> I suspect this is the problem.
>
> BTW, you should be checking whether the iteration has converged and if so, break out of the loop:
> if abs(d) < tol,break,end
> where tol is a number like 1e-6.
> Also, you should take the f= line outside the loop.
>


This is how I call the function:

 newtonmethod(@(t)[tan(t)-t], @(t)[(sec(t)^2)-1], 7, 1)

Subject: Funtion output

From: Nasser M. Abbasi

Date: 31 Aug, 2013 21:50:29

Message: 4 of 5

On 8/30/2013 11:29 PM, Ryan wrote:
> Hello,
>
> I have what I feel like is a simple question that for some reason I
>cannot find the answer too. I have a very simple function, which I
>will post below, and for some reason it will not print out all
>the variables when it is finished. In other words in the first
>line of the function I specify I want two outputs x and f.
>However, when I run it MATLAB only gives me x. Any help would be appreciated.
>
> function [x, f] = newtonmethod(fun,dfun,x0, nmax)
>
> for m =1:nmax
>
> fx = feval(fun,x0);
> dx = feval(dfun,x0);
> d = fx/dx;
> x= x0-d;
> f= feval(fun,x);
> %fprintf('x = %f f(x) = %f\n', x, f);
> x0 = x;
> end
>
> return
>
> Thanks,
> Ryan
>

This is how Matlab worked for 100 years or so now.

When you call a function that returns more than one separate
value, only the FIRST one is displayed UNLESS you capture
the rest explicitly, like this:

-----------------------------------
[x,f]=newtonmethod(@(t)[tan(t)-t], @(t)[(sec(t)^2)-1], 7, 1)
x =
    15.0700

f =
   -15.8114
------------------------------

If you just do:

-----------------------------------
newtonmethod(@(t)[tan(t)-t], @(t)[(sec(t)^2)-1], 7, 1)
ans =
    15.0700
---------------------------------

Then only the first output result is displayed.

--Nasser

Subject: Funtion output

From: Ryan

Date: 31 Aug, 2013 23:37:10

Message: 5 of 5

"Ryan " <saunders500@live.com> wrote in message <kvrreu$7dl$1@newscl01ah.mathworks.com>...
> Hello,
>
> I have what I feel like is a simple question that for some reason I cannot find the answer too. I have a very simple function, which I will post below, and for some reason it will not print out all the variables when it is finished. In other words in the first line of the function I specify I want two outputs x and f. However, when I run it MATLAB only gives me x. Any help would be appreciated.
>
> function [x, f] = newtonmethod(fun,dfun,x0, nmax)
>
> for m =1:nmax
>
> fx = feval(fun,x0);
> dx = feval(dfun,x0);
> d = fx/dx;
> x= x0-d;
> f= feval(fun,x);
> %fprintf('x = %f f(x) = %f\n', x, f);
> x0 = x;
> end
>
> return
>
> Thanks,
> Ryan


Thank you for the sarcasm and the information. Hope you have a great night. And thank you to everybody else who responded.

-Ryan

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us