Matlab Use ode23 ode solver with variable - matlab

I would like to use the ode23 solver for systems. My system has three equations and the first one depends on an variable 'z' which is declared in the code above.
HereĀ“s the sample code:
clc; clear;
z=1;
function Fv=funsys(t,Y,z);
Fv(1,1)=2*Y(1)+Y(2)+5*Y(3)+exp(-2*t) + z;
Fv(2,1)=-3*Y(1)-2*Y(2)-8*Y(3)+2*exp(-2*t)-cos(3*t);
Fv(3,1)=3*Y(1)+3*Y(2)+2*Y(3)+cos(3*t);
end
[tv,Yv]=ode23('funsys',[0 pi/2],[1;-1;0]);
The get the error that the variable 'z' is not declared.
So, is there any possibility to solve this?
(The function 'funsys' needs to be dependent on 'z')

Yes, have a look at Pass Extra Parameters to ODE Function in the documentation:
you can pass in extra parameters by defining them outside the function
and passing them in when you specify the function handle
In your case, it would look something like this:
[tv,Yv]=ode23(#(t,y) funsys(t,y,z),[0 pi/2],[1;-1;0]);

Related

How do I pass a polynomial variable into a matlab function?

I am just getting started with MATLAB and I have written a function to produce the binomial expansion of (x-a)^n when given x, a and n. As far as I can tell my code should work, but I do not seem to be using the function variables correctly.
function expand(a,n,x)
f = 0;
for k = 0:1:n
f = f + nchoosek(n,k).*x.^(n-k).*(-a).^k;
end
end
I need to be able to call the function and have it output f as the expanded polynomial in x, for example calling expand(1,3,x) should return x^3-3*x^2+3*x-1, but instead, calling it gives this error:
Unrecognized function or variable 'x'. It seems like it wants me to call the function with x being another number but I in fact need it to be able to be any letter to be used as the variable in the polynomial.
I know in Maple I would specify the variable type in the function to be x::name so I'm assuming there's something similar in MATLAB that I don't yet know.
Thanks for any help.
There are two ways to go about this:
Create x as a symbolic variable. For example, syms x; expand(a,n,x). This gives you the power of using the symbolic toolbox features like simplify(), but comes with a bit of a performance penalty. You should avoid using the symbolic toolbox in intensive calculations.
Return an anonymous function, f=#(X)sum(arrayfun(#(k)nchoosek....,1:n). This has better performance and does not require double(subs(...)) when you want an actual numeric value, but may be too difficult to implement for a beginner.

Add Explicit Derivative Function to Modelica.Math.Vectors.interpolate

I have a steady-state algebraic model (no der() expressions) with one non-linear equation. When translating in Dymola, the set of equations results in a numerical Jacobian. I would like to remove the numerical Jacobian (if possible) to improve performance of the model.
After setting the parameter Hidden.PrintFailureToDifferentiate=true (thanks to this tip from Claytex), I see that Dymola issues the warning:
Cannot find derivative of function:
InitializationParameterLoop.InitFunctions.interpolate(xVector, yVector, x, 1)
Note that the InitializationParameterLoop.InitFunctions.interpolate function is a copy of the Modelica.Math.Vectors.interpolate function, copied so that I can make modifications if necessary within my own package.
I attempted to add an explicit definition for the derivative dy/dx by making the following changes to the interpolate function:
Add the derivative annotation to the function definition:
function interpolate "Interpolate in a vector" extends Modelica.Icons.Function annotation(derivative = dydx);
Add a protected Real variable dydx:
Real dydx;
Calculate dydx in the algorithm section:
if abs(x2-x1)>0 then
dydx :=(y2 - y1)/(x2 - x1);
else
dydx := sign(y2 - y1)*Modelica.Constants.inf;
end if;
I was hoping that this simple method could be used to explicitly define dydx and remove the numerical Jacobian calculation, but it does not seem to make any change and Dymola still issues a warning about the
Am I misunderstanding the use of the derivative annotation? If so, can someone help me to understand how to define the derivative for the interpolate function?
Thank you!
This is the same issue as https://github.com/modelica/Modelica/issues/2078 and will be fixed in the next backward-compatible maintenance release of the Modelica Standard Library.

Solving ODEs inside a Subsystem in Simulink

I'm trying to figure out how to solve a system of ODEs inside a subsystem in a Simulink model. Basically, each call to this subsystem, which happens at each tick of the simulation clock (fixed-step), entails solving the ODEs. So there's like a different "clock" for the subsystem.
I have an M-file that implements the function for the system of ODEs. Currently, I have a MATLAB Function block for that. It needs a lot of parameters that I can get from the base workspace (via evalin and using coder.extrinsic('evalin') at the beginning). But I'm not allowed to define function_handle objects or inner functions to parameterize the function that is used by ode*. I think that if I'm able to solve the ODEs in this block, I'll have solved my problem. But those restrictions are "ruining" it.
I'd appreciate if you have any ideas of how to accomplish this. I welcome different approaches.
Thank you.
EDIT
A simple example is given below. It attempts to solve the van der Pol equation by changing the mu parameter randomly. This is the main idea I have at the moment, which doesn't work because of the problems mentioned above.
This is the main model with the subsystem:
This is the subsystem:
This is the MATLAB Function block implementation (note that there's an error in the # symbol, since defining function_handle objects isn't allowed):
Just use the MATLAB Function block as a wrapper. Put the bulk of your code into a "standard" MATLAB function (i.e. one callable from MATLAB, not the MATLAB Function block) and call that function (after defining it as coder.extrinsic) from the MATLAB Function block.
This will be a bit more complex than Phil Goddard's solution. The advantage is that it will allow you to generate standalone code, if necessary, whereas extrinsic functions are not compatible with standalone code generation.
The functions ode23 and ode45 are supported for code generation as of MATLAB R2014b so this applies if your MATLAB release is at least that new. Supposing so, the main limitation you are seeing is that anonymous functions are not supported for code generation.
Simulate Anonymous Function Parameters with Persistent Variables
However, these parameterized anonymous functions can be simulated using a normal function with a persistent. To simulate your function with the parameter mu, make a MATLAB file odefcn.m:
function x = odefcn(t,y)
%#codegen
persistent mu;
if isempty(mu)
% Adjust based on actual size, type and complexity
mu = 0;
end
if ischar(t) && strcmp(t,'set')
% Syntax to set parameter
mu = y;
else
x = [y(2); mu*(1-y(1)^2)*y(2)-y(1)];
end
Then in the MATLAB Function Block, use:
function y = fcn(mu)
%#codegen
% Set parameter
odefcn('set',mu);
% Solve ODE
[~,Y] = ode45(#odefcn,[0, 20], [2; 0]);
y = Y(end,1);
That should work both for simulation and for code generation. You can just add more arguments to odefcn if you need more parameters.

how to create a function to calculate hyperbolic sine of values

I am using the matlab 2007b and getting in a problem i want to make a function that takes input argument from the user and return sine hperbolic of that value.Sine hyperbolic should be define by that formula sinh(x)=(e^x-e^-x)/2
I am using that code while getting error
function sinh=sinhx(x)
% take the input value of x
a=exp(x)
% assinge exp(x) to a
b=exp(-1*x)
%assinge exp(-x) to variable b
c=a-b
%getting difference of these two variables
d=c/2
% dividing by 2 to get sinhx
end
kindly guide me how can I make this function or correct this code..thanks in advance for your assistance
The simpler the better: you should use the built-in sinh function.
Otherwise, in your function you don't define the output variable sinh, hence the error.
Best

How to pass a function to pdepe initial condition function

I have created a function from sets of points using curve fitting toolbox. I used generate code function and generated function called createFit(a,b), where a and b are sets of points used for interpolation. As a result createFit returns my interpolated function.
Now I want to use this function as the u0 (initial conditions) of my PDE equation (I am using pdepe to solve PDE). To do that, in function where I need to establish u0 I need to invoke a createFit function, which is not a problem, I have access to it. A problem is that I cannot pass a and b as the parameters to this function. I tried to make them global but it did not worked. How to do that?
From the pdepe documentation:
Parameterizing Functions explains how to provide additional parameters to the functions pdefun, icfun, or bcfun, if necessary.
Essentially, use nested functions or anonymous functions to handle extra parameters.