How do I set up an array of functions to use as an input

2 visualizaciones (últimos 30 días)
Ali1373
Ali1373 el 12 de Dic. de 2015
Editada: Ali1373 el 12 de Dic. de 2015
The provided example says to use
function [ G ] = ex( X )
G = [(-0.1*X(1)^3 + 0.1*X(2) + 0.5) (0.1*X(1) + 0.1*X(2)^3 +0.1)]
as the input for function x = FixedptSys( G, X0, tol)
which gives me an error no matter how I set up the function array
in FixedptSys I'm trying to do feval(G, X0)'; where X0(1) and X0(2) are given and supposedly I should get an array back
I've gone as far as doing :
function G = ex( X)
F(1).fun = inline(['-0.1*x^3 + 0.1*y + 0.5']);
F(2).fun = inline(['0.1*x + 0.1*y^3 +0.1']);
G(1) = feval(F(1).fun,X(1),X(2));
G(2) = feval(F(2).fun,X(1),X(2));
end
and saving it as a function but every time I try to input it FixedptSys( G, X0, tol)
x = FixedptSys( G, X0, tol) why I try to call it with G , it runs it and says not enough input
Error in G (line 2) display(X)
Any similar implementation method would work though I'd love to find out how the example has it
thanks in advance
Also here is Fixpoint
function x = FixedptSys( G, X0, tol)
% use fixed=point iteration to solve G(x) = x
% assuming G has a soln (meets condition 1 and 2 )
% if G is a system fo equations it shoudl accept a vector
%%first iteration of x and error
xnew = feval(G, X0)';
err = norm(xnew - X0);
% alterinatively err(1) and susbequtenly err(i) maybe used to save
% iterations
%error check
while abs(err) <= tol
%%finding the next x and err
xnew = feval(G, X0)';
err = norm(xnew - X0);
X0 = xnew;
end
%%Final solution
x = xnew;
% given the theorem 2.3 numerical computations, this is gurantted to give an answer
% if intial contions are met so no need to code the failure case
end

Respuesta aceptada

Image Analyst
Image Analyst el 12 de Dic. de 2015
You're getting the name of the returned variable, the name of the function, and the name of the m-file all mixed up. The name of that function is ex(), NOT G(). G is a 2 element array that is the output of ex(). So your m-file should also be ex.m, not G.m and (I think) you should pass @ex, which is a handle to the function, into FixedptSys() not G, which is simply a 2 element array.
  1 comentario
Ali1373
Ali1373 el 12 de Dic. de 2015
Editada: Ali1373 el 12 de Dic. de 2015
O my god yes that @ was magic! thankyou!
Also i have actual file saved as G so that solves that

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Function Creation en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by