How to index a value of an anonymous function

I am trying to create a function that uses Euler Method to find the height of water in a tank as a function of time given the equation for dy/dt. The equation dy/dt is to be passed into my function as an anonymous function. The equation for dy/dt depends on both y and t as well as some constants.
My program initilizes a vector called change that I want to store the values of dy/dt as the loop runs. I cannot run the program as it is given because I am trying to store each value of dy/dt (which should be calculated on each iteration based on the changing value of y as the loop runs) to be multiplied by the timestep, but the value of the vector called 'change' is remaining constant. What I essentially want to do is have 'change(k)=dydt(k)' but because I am passing dydt as an anonymous function using 'y' as an input I cannot simply index using '(k)' at the end of the vector name the way I usually would. I'm not sure if there is an easy fix or if I need to re-program the way the way I am using the anonymous function. Any tips and help are appriciated, thank you!!
function [depth_vector] = Depth_Calc(dydt,i,f,n,varargin)
%Calculates the Depth vs Time by evaluating the differential
%equation y with parameters A, Q, alpha from time i to time f
%using timestep n and generates table printout of results
%input:
%dydt= function (given as anonymous function)
%i=initial time
%f=finl time
%n=timestep
t=[i:n:f];
y=ones(1,((f-i)/n));
change=.32635*ones(1,((f-i)/n));
for k=2:((f-i)/n)
change=dydt(varargin{:})
y(k)=y(k-1)+n*change(k-1);
end
depth_vector=y;
output=depth_vector;

2 Commenti

You want change to be a matrix? According to your code y also will be a matrix.
y is a function of t
dydt is a function of both t and y
I really only need the output of y as a matrix or vector, as the end result I am looking for are the values of y vs time. The values for dydt are only temporarily needed to calculate the next value of y inside the loop. When I tried to use the function dydt directly in the Euler method (instead of first assigning the value it to variable 'change') I got the error that the number of array elements between y and dydt did not match.

Accedi per commentare.

 Risposta accettata

The exact solution depends on what are the input arguments of dydt, but if it is defined as an anonymous function, then you should be able to do something like this inside the for-loop
change(k) = dydt(varargin{:})

6 Commenti

This is what I had as my original solution but when I run the program I get the following error message:
Unable to perform assignment because the left and right sides have a
different number of elements.
Error in Depth_Calc (line 18)
change(k)=dydt(varargin{:});
The input arguments for dydt are t and y , the time and depth, both defined within the function program. There are 3 constants as input arguments as well, which are defined in the workspace.
So does that mean dydt accept 5 inputs? Currently, you are not passing y and t to dydt. You can call it like this
change=dydt(t(k), y(k-1), varargin{:})
When using the function I start by entering the following into the command window before calling the actual function:
>> dydt=@(t,y,A,Q,a) 3*(Q/A).*((sin(t)).^2)-(a.*((1+y).^1.5))./A;
>> A=1300 ; Q=450 ; a=150;
Then I pass dydt as an input into the function.
I do not get an error message that dydt does not have enough input arguments when I use it this way. I thought that the program would look for the values I assigned to t and y within the actual program code and use those as inputs for dydt since they were listed as inputs in the command window. Do I need to add the additional line you sugested inside of my program code to 'tell' the anonymous function to use these new values for t and y assigned within the program as inputs?
Yes, you need to specify the inputs explicitly. Anonymous function will not automatically pick variables from the current workspace. Also, defining A, Q, and a here as the input seems unnecessary, but maybe you want to modify it later. The following shows how to write this correctly
function [depth_vector] = Depth_Calc(dydt,i,f,n,varargin)
%Calculates the Depth vs Time by evaluating the differential
%equation y with parameters A, Q, alpha from time i to time f
%using timestep n and generates table printout of results
%input:
%dydt= function (given as anonymous function)
%i=initial time
%f=finl time
%n=timestep
t=i:n:f;
y=ones(1,((f-i)/n));
change = .32635*ones(1,((f-i)/n));
for k=2:((f-i)/n)
change(k) = dydt(t(k), y(k-1), varargin{:});
y(k)=y(k-1)+n*change(k);
end
depth_vector=y;
output=depth_vector;
end
Run it like this
dydt=@(t,y,A,Q,a) 3*(Q/A).*((sin(t)).^2)-(a.*((1+y).^1.5))./A;
A=1300 ; Q=450 ; a=150;
f = Depth_Calc(dydt, 1, 10, 1, A, Q, a)
This works perfectly thank you so much! I do have one question though, just for my understanding:
If I don't employ 'f=' before calling the function I get an error message of 'too many input arguments'
Why do I have to assign the function to a variable rather than just calling it?
I don't think that is the issue. Following code works just fine for me
dydt=@(t,y,A,Q,a) 3*(Q/A).*((sin(t)).^2)-(a.*((1+y).^1.5))./A;
A=1300 ; Q=450 ; a=150;
Depth_Calc(dydt, 1, 10, 1, A, Q, a)
Can you show the lines of code that causes this error?

Accedi per commentare.

Più risposte (0)

Prodotti

Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by