Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined function or variable Matlab

I need to write a fixed point iteration algorithm and it's mostly coded but I'm running into this error:

Undefined function or variable 'g'.

I want to define g as g(x) = 1/2*(1+5/x). However, I'm a matlab n00b and unsure how to do this. Any help would be much appreciated, thanks.

  function [y,k] = fixedpoint(g,p0,tol,max1)
          for k=1:max1
          p = g(p0);
          err = abs(p-p0);
          abserr = abs(sqrt(5)-p);
          ratioerr = abserr/(abs(sqrt(5)-p0));

          if (err<tol)
             break
          end
          p0 = p;

  end
  if (k==max1)
      disp('The algorithm did not converge')
  end
  y = p;
like image 655
user2803016 Avatar asked Mar 18 '26 03:03

user2803016


1 Answers

You can define g as a anonymous function

g = @(x)1/2*(1+5/x)

Then call your function fixedpoint in usual way

like image 61
P0W Avatar answered Mar 20 '26 01:03

P0W



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!