Matlab script "sample time period" error when calling Simulink model - matlab

I am writing a Matlab script to call my Simulink model :
Constant10=43;
Constant11=43;
In1=[1,2,3];
In2=[4,5,6];
t_stop = 100;
T_s = t_stop/1000;
options = simset('solver', 'ode5', 'fixedstep', T_s);
sim('test_lau.slx',t_stop,options)
But I get the following error:
Error using test_call_model (line 18)
The sample time period (0.01) of 'test_lau/INES0' is not an integer multiple of the fixed step size (0.1) specified for model.
Does anyone know how to solve it? I tried adding the line
T_s= int64(T_s)
But I have the same issue...
Does anybody know how to solve this?
Thank you !

Someone answered it for me!
Because 'test_lau/INES0' this block sample time (0.01) is lower than model sample time.
It sould be multiple of the fixed step size (0.1) specified for model.
there are 2 options
1)set model sample time as 0.01
2)set 'test_lau/INES0' sample time as inherit ot 0.1

Related

Matlab ode45 internal step

I'm having doubts at how to use ode45 since I know it uses an internal variable step size. For instance, in my particular case I have a model of ODE's and I use a sampling time of 5 minutes. Hence, inside my main simulation loop I have the following line to obtain the output of my model by solving it using ode45:
[T,X] = ode45(#(t,x) model(t,x,u,data),[t t+scenario.Ts],x0);
Where u are inputs of the model, data is a structure with parameters, x0 are the initial conditions at the current time step and [t t+scenario.Ts] is the initial and final time. My doubt is that between t and t+scenario.Ts the ode45-solver uses variable time steps and thus the way I introduce my input actions u may be affected. Hence, I understand that a value of a particular input u is kept constant through the internal time steps between [t t+scenario.Ts]. Then, if I have for instance a flux, i.e. water into a tank, the time step has a direct effect to this u.
Let me explain this a little more with an example. If over [t t+scenario.Ts] I know that u(1) = 10. Then the real input I should use is u(1)=10/(# of time steps between [t0 tend]). However, since the internal step is variable which input do I have to use?
I hope you understand my problem and can help me out.
You should formulate the inputs such that it is independent of the time- discretization. This shouldn't be a problem if your equations are formulated in continuous time. If the control variable is not constant, then you should be make it explicitly dependent of t and write a function u(t).
If my answer is not sufficient to help you out, please add more details about your application, especially the dynamical model that you are simulating. Then we can help you further.

Using a Jacobian with MatCont

I am trying to solve a very large non-linear problem using the MatCont package. Due to the large number of dimensions, and the non-linear nature, I believe that supplying the Jacobian for my system to the MatCont algorithm will speed things up immensely. However, I cannot get it to recognise that it has a Jacobian to use!
As a minimal working example, I have modified the circle finder from the help documentation to include a Jacobian:
function out = curve()
out{1}=#curvefunc;
out{2}=#defaultprocessor;
out{3}=#options;
out{4}=#jacobian;
out{13}=#adapt;
end
function f=curvefunc(x)
f=x(1)^2+x(2)^2-1;
end
function J=jacobian(x)
disp('USE JACOBIAN')
J=[2*x(1) , 2*x(2)];
end
function varargout=defaultprocessor(varargin)
if nargin>2
varargout{3}=varargin{3};
end
varargout{2}=[0];
varargout{1}=0;
end
function option=options()
option=contset;
end
function [res,x,v]=adapt(x,v)
res=[];
end
I then try to run this program from the command line using
[x,v,s,h,f] = cont(#curve,[1;0]);
However, the response is
first point found
tangent vector to first point found
elapsed time = 0.2 secs
npoints curve = 300
Since I told it to output 'USE JACOBIAN' every time the Jacobian function was called, it is clear that MatCont is not using it.
How do I get the Jacobian to be used?
I solved my own problem! Seems I was quite close to getting it working. The below is a bit of a botch, so if anyone knows how to do it with options please post an answer also.
Firstly I edit the options settings so that when it performs the continuation it only locates the first point:
function option=options()
option = contset;
option = contset(option,'MaxNumPoints',1);
end
its fine for it to do this using numerical Jacobians, the first point is known very well in most problems. This is then called from a script or a function using the following:
[x,v,s,h,f] = cont(#curve,[1;0]);
global cds
cds.options.MaxNumPoints=[];
cds.symjac=1;
[x,v,s,h,f] = cont(x,v,s,h,f,cds);
The first line finds the initial point using numerical Jacobians, as it was set up to do. The continuer is then manually adjusted to firstly have no limit on the maximal number of points (this can be set to any appropriate number) and then the use of the user provided Jacobian is set to 1 (true). The continuer is then resumed with the new settings and uses the Jacobain properly.

Reading a file that corresponds to time step in solving ODE

I'm trying to solve an ODE in Matlab and I have the following problem:
my code is as bellow:
xinit=[0.19;25;0;7];
t=0:768:76800; %% 101 cells
[t1,y]=ode45(#Model_Bio,t,xinit);
In the function #Model_Bio I have a parameter that I need to read its corresponding values for each time step (101 values)!
So, My #Model_Bio is somehow as bellow:
load 'mydata'
a=mydata;
.....
the problem is that, when I execute the ode45(#Model_Bio,t,xinit), it calls my function with an automated time step (for example 50 times!) and my problem is that I cannot assign the values for each of my time step (101)!
Also, I think its not a good idea to change the time step of the ode the same as my 101 steps!
Anyone that help me on this is really appreciated!
It seems like you need to provide a wrapper of your data that interpolates it for arbitrary t, for example
my_interp = #(t) interp1(my_data_t, my_data_x, t)
http://se.mathworks.com/help/matlab/ref/interp1.html
and then implement your RHS (#Model_Bio) in terms of my_interp

Matlab libsvm svmpredict accuracy verbose

I have a question of an annoying fact. I'm using libsvm with matlab and I'am able to predict using:
predicted_label = svmpredict(Ylabel, Xlabel, model);
but it happen that every time I make a predictions appears this:
Accuracy = X% (y/n) (classification)
Which I find annoying because I am repeating this procedure a lot of times and also makes it slow because its displaying in screen.
I think what I want is to avoid that svmpredict being verbose.
Can anyone help me with this? Thanks in advance.
-Jessica
I found a much better approach than editing the source code of the c library was to use matlabs evalc which places any output to the first output argument.
[~ predicted_label] = evalc('svmpredict(Ylabel, Xlabel, model)');
Because the string to be evaluated is fixed should be no performance decrease.
svmpredict(Ylabel, Xlabel, model, '-q');
From the manual:
Usage: [predicted_label, accuracy, decision_values/prob_estimates] = svmpredict(testing_label_vector, testing_instance_matrix, model, 'libsvm_options')
[predicted_label] = svmpredict(testing_label_vector, testing_instance_matrix, model, 'libsvm_options')
Parameters:
model: SVM model structure from svmtrain.
libsvm_options:
-b probability_estimates: whether to predict probability estimates, 0 or 1 (default 0); one-class SVM not supported yet
-q : quiet mode (no outputs)
If you are using matlab, just find the line of code that is displaying this information (usually using 'disp', 'sprintf', or 'fprintf') and comment it out using the commenting operator %.
example:
disp(['Accuracy= ' num2str(x)]);
change it to:
% disp(['Accuracy= ' num2str(x)]);
If you are using the main libsvm library then you need to modify it before making.
1- Open the file 'svmpredict.c'
2- find this line of code:
info("Accuracy = %g%% (%d/%d) (classification)\n",
(double)correct/total*100,correct,total);
3- just comment it out using // operator
4- save and close the file
5- make the project

error related to white noise generation

i have a problem and please help me to fix it.suppose that we have following data
f1=100;
f2=200;
T=1./f1;
N=3;
m=500;
t=(0:(N*T)/m:(N*t))';
wn=rand(length(t),1).*2-1;
but when i tried to see value of wn,it shows me following one
0.6294
i need it for following code
x = 20.*sin(2.*pi.*f1.*t) + 30.*cos(2.*pi.*f2.*t) + A3.*wn;
A3 is amplitude for white noise.but main problem is follwing
1.should not wn be array instead of scalar?
2.why it shows me the same value of wn at each time i type wn?
as you see this is model of two deterministic periodic model +white noise
t=(0:(N*T)/m:(N*t))';
t is using itself in it's definition. That can't be right. I would suspect if you called clear and then run this script that line would error.