matlab ode45 how to change a parameter inside the function continiously - matlab

i am trying to solve a differential equation using ode45, I have a function in which one of the parameters has to vary by specific step, here is my function:
function f=RSJ(y,t,M1,P,M2,E,current)
f=(current/P)-(M1/P)*sin(y)+(M2/P)*sin(y+E);
P, M1, M2 & E are numerical constants, current is the parameter which I should resolve this differential equation for several cases, for example current=0:1:10
how can I do such a thing?

Use a closure (a.k.a. anonymous or lambda function):
% declare t, y, tspan and y0
% [...]
current = 6e-7 : 1e-8 : 8.5e-7;
for k=1:length(current)
f = #(y, t, M1, P, M2, E) (current(k)/P)-(M1/P)*sin(y)+(M2/P)*sin(y+E);
[t{k}, y{k}] = ode45(f, tspan, y0);
end

A quick and dirty solution. Define current as a global variable (you need to do this in both the base workspace and in the function) and use a for loop, e.g.
current_vector=1e-7*(6:0.1:8.5);
global current
for k=1:length(current_vector)
current = current_vector(k);
[t{k},y{k}]=ode45(f,<tspan>,<y0>)
end
Replace <tspan> and <y0> by the appropriate values/variables. I assume the other constants are defined in the body of the function, in which case your function should look something like this:
function f=RSJ(t,y)
global current
M1 = ... % etc...
f=(current/P)-(M1/P)*sin(y)+(M2/P)*sin(y+E);
end
BTW, I don't see any explicit dependence on time t in your function...

Related

How to use fzero() to solve polynomial equation in MATLAB?

I would like to solve the following polynomial numerically for r:
I am trying to use fzero() as follows:
r = (5/(r^2*9))- ((2)/(9*(6-r)^2))
x0 = 10; % some initial point
x = fzero(r,x0)
How can this be done with fzero()?
Input variable and function name should be different
Just change the function name to pol
To use fzero the function pol have to be a function handle
defined via #
pol =#(r) (5/(r^2*9))- ((2)/(9*(6-r)^2))
x0 = 10; % some initial point
x = fzero(pol,x0)
solution
x = 3.6754
It should be noted that, the first argument in fzero() should be "a function handle, inline function, or string containing the name of the function to evaluate", but yours is just an expression, which is not valid.
Besides the approach by #Adam (using function handle), another way is to use anonymous function, i.e.,
x = fzero(#(r) (5/(r^2*9))- ((2)/(9*(6-r)^2)) ,x0)
where
#(r) (5/(r^2*9))- ((2)/(9*(6-r)^2))
is the anonymous function with respect to argument r.
You will get the same result that x = 3.6754.

ODE solver producing runtime error - not enough input arguments [duplicate]

I have a use case as follows:
Inside F.m I have a function F that takes as its argument a 2 x 1 matrix x. F needs to matrix multiply the matrix kmat by x. kmat is a variable that is generated by a script.
So, what I did was set kmat to be global in the script:
global kmat;
kmat = rand(2);
In F.m:
function result = F(x)
global kmat;
result = kmat*x;
end
Then finally, in the script I have (x_0 has already been defined as an appropriate 2 x 1 matrix, and tstart and tend are positive integers):
xs = ode45(F, [tstart, tend], x_0);
However, this is causing the error:
Error using F (line 3)
Not enough input arguments.
Error in script (line 12)
xs = ode45(F, [tstart, tend], x_0);
What is going on here, and what can I do to fix it? Alternatively, what is the right way to pass kmat to F?
Firstly, the proper way to handle kmat is to make it an input argument to F.m
function result = F(x,kmat)
result = kmat*x;
end
Secondly, the input function to ode45 must be a function with inputs t and x (possibly vectors, t is the dependent variable and x is the dependent). Since your F function doesn't have t as an input argument, and you have an extra parameter kmat, you have to make a small anonymous function when you call ode45
ode45(#(t,x) F(x,kmat),[tstart tend],x_0)
If your derivative function was function result=derivative(t,x), then you simply do ode45(#derivative,[tstart tend],x_0) as Erik said.
I believe F in ode45(F,...) should be a function handle, i.e. #F. Also, you can have a look at this page of the MATLAB documentation for different methods to pass extra parameters to functions.

Matlab: Meaning of #(t)(costFunction(t, X, y)) from Andrew Ng's Machine Learning class

I have the following code in MATLAB:
% Set options for fminunc
options = optimset('GradObj', 'on', 'MaxIter', 400);
% Run fminunc to obtain the optimal theta
% This function will return theta and the cost
[theta, cost] = ...
fminunc(#(t)(costFunction(t, X, y)), initial_theta, options);
My instructor has explained the minimising function like so:
To specify the actual function we are minimizing, we use a "short-hand"
for specifying functions, like #(t)(costFunction(t, X, y)). This
creates a function, with argument t, which calls your costFunction. This
allows us to wrap the costFunction for use with fminunc.
I really cannot understand what #(t)(costFunction(t, X, y) means. What are the both ts are doing? What kind of expression is that?
In Matlab, this is called an anonymous function.
Take the following line:
f = #(t)( 10*t );
Here, we are defining a function f, which takes one argument t, and returns 10*t. It can be used by
f(5) % returns 50
In your case, you are using fminunc which takes a function as its first argument, with one parameter to minimise over. This could be called using
X = 1; y = 1; % Defining variables which aren't passed into the costFunction
% but which must exist for the next line to pass them as anything!
f = #(t)(costFunction(t, X, y)); % Explicitly define costFunction as a function of t alone
[theta, cost] = fminunc(f, 0, options);
This can be shortened by not defining f first, and just calling
[theta, cost] = fminunc(#(t)(costFunction(t, X, y)), 0, options);
Further reading
As mentioned in the comments, here is a link to generally parameterising functions.
Specifically, here is a documentation link about anonymous functions.
Just adding to Wolfie's response. I was confused as well and asked a similar question here:
Understanding fminunc arguments and anonymous functions, function handlers
The approach here is one of 3. The problem the anonymous function (1 of the 3 approaches in the link below) solves is that the solver, fminunc only optimizes one argument in the function passed to it. The anonymous function #(t)(costFunction(t, X, y) is a new function that takes in only one argument, t, and later passes this value to costFunction. You will notice that in the video lecture what was entered was just #costFunction and this worked because costFunction only took one argument, theta.
https://www.mathworks.com/help/optim/ug/passing-extra-parameters.html
I also had the same question. All thanks to the the link provided by Wolfie to understand paramterized and anonymous functions, I was able to clarify my doubts. Perhaps, you must have already found your answer but am explaining once again, for people who might develop this query in the mere future.
Let's say we want to derive a polynomial, and find its minimum/maximum value. Our code is:
m = 5;
fun = #(x) x^2 + m; % function that takes one input: x, accepts 'm' as constant
x = derive(fun, 0); % fun passed as an argument
As per the above code, 'fun' is a handle that points to our anonymous function, f(x)=x^2 + m. It accepts only one input, i.e. x. The advantage of an anonymous function is, one doesn't need to create a separate program for it. For the constant, 'm', it can accept any values residing in the current workspace.
The above code can be shortened by:
m = 5;
x = derive(#(x) x^2 + m, 0); % passed the anonymous function directly as argument
Our target is to find the global optimal,so i think the function here is to get a bounch of local minimal by change the alpha and compare with each other to see which one is the best.
to achive this you initiate the fminuc with value initial_theta
fminuc set t=initial_theta then compute CostFunction(t,X,y) which is equal to` CostFunction(initial_theta,X,y).you will get the Cost and also the gradient.
fminuc will compute a new_theta with the gradient and a alpha, then set t=new_theta and compute the Cost and gradient again.
it will loop like this until it find the local optimal.
Then it change the length of alpha and repeat above to get another optimal. At the end it will compare the optimals and return with the best one.

How to use derive.m function to get central differentioation of f(x)

I've a function that can calculate central difference of f(x) but I'm basic user of matlab and I can't define f(x) for function I've mentioned.
Can any body help me and define f(x) for this function?
INPUTS are a function f; a value
of h; a specific point a; the number of rows n. The input function f(x)
should be defined as an M-file.
My f(x) is : S(x) = 10*x-sin(x) with steps: 0.1
I want to get df(x) near the piont=pi/6
I wrote:
clc
syms x
derive(10*x-sin(x),0.1,pi/6,14)
but I've got this error :
Error using feval
Argument must contain a string or function_handle.
Error in derive (line 7)
D(1,1)=(feval(f,a+h)-feval(f,a-h))/(2*h);
Error in centre(line 2)
========================================
the function derive.m is here:
function derive(f,h,a,n)
% Approximate the derivative of a function at x = a.
disp(' Derivative table')
disp('______________________________________________')
disp(' i h Di,1 Di,2 Di,3 ... ')
disp('______________________________________________')
D(1,1)=(feval(f,a+h)-feval(f,a-h))/(2*h);
fprintf('%2.0f %8.4f %12.4f\n',1,h,D(1,1));
for i=1:n-1
h=h/2;
D(i+1,1)=(feval(f,a+h)-feval(f,a-h))/(2*h);
fprintf('%2.0f %8.4f %12.4f',i+1,h,D(i+1,1));
for k=1:i
D(i+1,k+1)=D(i+1,k)+(D(i+1,k)-D(i,k))/((4^k)-1);
fprintf('%12.4f',D(i+1,k+1));
end
fprintf('\n');
end
feval expects a function handle, that is a MatLab function handle. What you provided (10*x-sin(x)) is a symbolic expression.
You can define a function handle as follows:
f(x) = 10*x-sin(x);
Now f is a MatLab function handle, and you can use it as you wish:
derive(f,0.1,pi/6,14)
EDIT
You can also use an anonymous functions
derive((x)(10*x-sin(x)),0.1,pi/6,14)
END EDIT
Note, you do not need syms x, it defines x as a symbol, but in your function definition you use the parameter x defined in f(x).
EDIT
If you want to use a symbolic function, you can convert it to a MatLab function handle using matlabFunction

Matlab error while running

I am trying to implement the following code in Matlab:
v0=eps;
t0=0; tf=10;
[t,v]=ode45(#const_pow, [t0 tf], v0);
And the const_pow function is:
function f=const_pow(t,v)
f=(P/v-F0-c*v^2)/m;
end
While I am running the program, the following error shows up:
Error using ==> odearguments at 103
CONST_POW returns a vector of length 0, but the length of initial
conditions vector is 1. The vector returned by CONST_POW and the
initial conditions vector must have the same number of elements.
Any suggestions on where the error might be?
From your comments, you're defining some variables globally. In order to access them inside const_pow, you need to tell that function to use the global variables, e.g.:
function f=const_pow(t,v)
global P F0 c m
f=(P/v-F0-c*v^2)/m;
end
Without doing this, the function will not touch the global variables (this is to stop you accidentally changing their values). For example:
function f=const_pow(t,v)
global P F0 c
m = 23.7; % this does not change the global value of m
f=(P/v-F0-c*v^2)/m;
end
An alternative to passing a number of variables without using globals is to define a structure with your required data:
data.P = % initial values
data.F0 =
data.c =
data.m =
This also gives you a little more flexibility if there is more than one set of options. Or, you could pass the variables in directly. To do this with ode45, you would need to parameterize the function using an anonymous function:
function f=const_pow(t,v,P,F0,c,m)
% and so on
end
With P,F0,c,m pre-defined:
odefun = #(t,v) const_pow(t,v,P,F0,c,m); % anonymous function
Then:
[t,v]=ode45(odefun, [t0 tf], v0);