Converting function file to Anonomys function.

Given we have the function file
function y = lorenz(t,x,sigma,rho,beta)
% INPUT: t is a a real value indicating time
% x is a column vector of size 3 x 1
% sigma, rho, beta are parameters of the Lorenz
% equations
% OUTPUT: y is a column vector of size 3 x 1 that gives
% the right hand side of the Lorenz equations
y=[0;0;0];
x=x+t;
y(1)=sigma*(x(2)-x(1));
y(2)=x(1)*(rho-x(2))-x(3);
y(3)=x(1)*x(2)-beta*x(3);
end
How can we convert this into a Anonymous function
f = @(t,x) lorenz(t,x,sigma,rho,beta)
??.

6 Commenti

Stephen23
Stephen23 il 1 Giu 2015
Modificato: Stephen23 il 1 Giu 2015
What is wrong with f = @(t,x) lorenz(t,x,sigma,rho,beta) ? This seems to be the answer you are looking for (as long as sigma, rho and beta are defined in the workspace)... or is there something unexpected happening?
According to the original question there is no need to use feval or the like. That would be slow and not very robust.
at the moment I am getting " Error in lorenz Too many input argumnets"
%This is my main.m function.
% main.m, Script to test all of the functions
%PART 1.
%Set the values, rho, sigma and beta.
rho=28;
sigma = 10;
beta= 8/3;
%Part 2
%Define the right hand side of the Lorentz system (1)
%as an anonymous function.
f = @(t,x) lorenz(t,x,sigma,rho,beta);
%f = @(t,x) lorenz(t,x,sigma,rho,beta);
%PART 3
%Define a vector of equally spaced grid points
k=1;
h=10^(-k);
tfinal=1;
t=[0:h:tfinal];
%PART4
%Set the initial data points for y0.
y0=[-1;3;4];
%PART 5
%Ask the user which method is to be used to
%Solve the system.
prompt='Please enter method you wish to use to solve system'
disp('Eulers Method (EM)');
disp('Runge-Kutta order 4 (RK4)');
disp('Implicit Runge-Kutta Method (IRK4)');
method=input(prompt,'s');
%PART 5
%Solve the systmem using the method which the user
%has chosen. Store the results in a matrix Y of size
%3*N, where N is the length of the vector t.
switch method
case 'EM'
disp('Euler Method');
y= EulerSolver(f,t,y0);
case{ 'RK4'}
printf('Runge-Kutta Method\n');
case{ 'IRK4'}
printf('Implicit Runge Kutta method');
otherwise
disp('Wrong input format');
end
This is my EulerSolver Function
function [ tout, yout ] = EulerSolver( f,t,y0 )
% INPUT: f(t,y) is an anonymous function that defines
% the right-hand side of the ODE ydot = f(t,y)
% t =[t0 t1 ... tfinal] is a vector of grid points
% with length N
% y0=[a b c] is a column vector that contain the
% initial values x(0)=a, y(0)=b, z(0)=c.
% OUTPUT:tout is a column vector of grid points.
% yout is an 3 x N matrix containing the solution
% at different grid points.
N=numel(t);
yout=zeros(3,N);
yout(:,1)=y0;
for n=2:N
h=t(n)-t(n-1);
yout(:,n)=yout(:,n-1)+h.*f(t(n-1),yout(:,n-1));
end
tout=t(n);
end
Please show the complete trace of the "too many input arguments" error.
If you are getting an error message please give us the complete message (i.e. all of the red text). We need this to know what is happening.
Also please do not insert empty lines into your code, it makes it difficult to read on this forum.

Accedi per commentare.

Risposte (2)

If you're asking how to replace the whole code in your lorenz function by an anonymous function, I'm not sure it's a good idea as it's not going to be very readable due to the severe limitations of anonymous functions in matlab.
This would be something like:
f = @(t, x) [sigma*(t+(x(2)-x(1))), (x(1)+t)*(rho-x(2)-t)-x(3)-t, (x(1)+t)*(x(2)+t)-beta*(x(3)+t)];
Keep your function in a file. It's a lot clearer.
Nobel Mondal
Nobel Mondal il 1 Giu 2015
Modificato: Nobel Mondal il 1 Giu 2015
fH = str2func('lorenz');
y = feval(fH,t,x,sigma,rho,beta);

1 Commento

Is the second line supposed to read f= feval(fH,t,x,sigma,rho,beta);
?

Accedi per commentare.

Categorie

Richiesto:

il 1 Giu 2015

Commentato:

il 2 Giu 2015

Community Treasure Hunt

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

Start Hunting!

Translated by