COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### convert function files to inline functions?

• Email
• Follow

```Hello

Is it possible to write a function, which is written in a file as an inline or anonymous function.
The function in question is for example
function [t,x]= euler(f,interval,xo,N)
h=(interval(2)-interval(1))/N;
t=zeros(1,N+1);
t(1)=interval(1);
x=zeros(size(xo,1),N+1);
x(:,1)=xo(:);
for k=2:N+1
s=feval(f,t(k-1),x(:,k-1));
s=s(:);
t(k)=interval(1)+(k-1)*h;
x(:,k)=x(:,k-1)+h*s;
end

I can't see how to write that in the form
f=@(t,x)[-50*(x-cos(t))];

thanks

Uwe Brauer
```
 0
Reply oub (94) 7/5/2009 6:37:01 PM

See related articles to this posting

```"Uwe Brauer" <oub@mat.ucm.es> wrote in message
news:h2qrsd\$cpq\$1@fred.mathworks.com...
> Hello
>
> Is it possible to write a function, which is written in a file as an
> inline or anonymous function.

Unless it's a simple function, probably not.

> The function in question is for example
> function [t,x]= euler(f,interval,xo,N)
> h=(interval(2)-interval(1))/N;
> t=zeros(1,N+1);
> t(1)=interval(1);
> x=zeros(size(xo,1),N+1);
> x(:,1)=xo(:);
> for k=2:N+1
>    s=feval(f,t(k-1),x(:,k-1));
>    s=s(:);
>    t(k)=interval(1)+(k-1)*h;
>    x(:,k)=x(:,k-1)+h*s;
> end
>
> I can't see how to write that in the form
> f=@(t,x)[-50*(x-cos(t))];

So you have the function above in euler.m and you want to call it on the
function g(t,. x) = -50*(x-cos(t))?

g = @(t, x) -50*(x-cos(t));
[t, x] = euler(g, [0 10], 1, 100);

I made up the values [0 10], 1, and 100 as the latter three inputs to euler;
replace these with appropriate values for your problem.

--
Steve Lord
slord@mathworks.com

```
 0
Reply slord (13686) 7/5/2009 11:09:32 PM

```>>>>> "Steven" == Steven Lord <slord@mathworks.com> writes:

> "Uwe Brauer" <oub@mat.ucm.es> wrote in message
> news:h2qrsd\$cpq\$1@fred.mathworks.com...
>> Hello
>>
>> Is it possible to write a function, which is written in a file as an
>> inline or anonymous function.

> Unless it's a simple function, probably not.

>> The function in question is for example
>> function [t,x]= euler(f,interval,xo,N)
>> h=(interval(2)-interval(1))/N;
>> t=zeros(1,N+1);
>> t(1)=interval(1);
>> x=zeros(size(xo,1),N+1);
>> x(:,1)=xo(:);
>> for k=2:N+1
>> s=feval(f,t(k-1),x(:,k-1));
>> s=s(:);
>> t(k)=interval(1)+(k-1)*h;
>> x(:,k)=x(:,k-1)+h*s;
>> end
>>
>> I can't see how to write that in the form
>> f=@(t,x)[-50*(x-cos(t))];

> So you have the function above in euler.m and you want to call it on the
> function g(t,. x) = -50*(x-cos(t))?

No, sorry I did not explain that too well I am afraid:
I want to have ONE file to rule them all.

That is one file, say all.m which contains

-  the definition of euler,

-  the definition of g

-  and the call of g with concrete parameters.

Then I simply run all.

g is an inline function so there is no problem, but euler right now is
not and  I don't know how to rewrite euler as an inline function.

If I call all then of course matlab complains since I call a function
as a script

Uwe Brauer
```
 0
Reply oub (94) 7/6/2009 9:28:16 AM

2 Replies
39 Views

Similar Articles

12/6/2013 12:13:21 AM
[PageSpeed]