How can I implement the following blocks and code in function block ?

I would use the following integral equation as function block in my simulation. I know I can do it as what I attached but I need a way to implement it by code inside function block.

2 个评论

I have read it but you know the simlink will run the blocks at each instant but in the link the result will give you over one period. Sorry I=the link cannot help me.
Just consider M=4 ; N=2 and I have a clock start from t=0:0.1:3, I need the y(t) to have all value at each t=0:0.1:3;

请先登录,再进行评论。

回答(1 个)

This is an example of matlab given in integral help
% Without For loop
fun = @(x,c) 1./(x.^3-2*x-c);
q_f = integral(@(x)fun(x,5),0,2);
q_f = -0.4605
Now i just change the time of integration and sum them
% With For loop
t = 0:0.1:2;
q =0;
for i =2:length(t)
q =q+ integral(@(x)fun(x,5),t(i-1),t(i));
end
q = -0.4605

6 个评论

Thank you , but you still do not answer the question. This result will be over the whole period of time, but what I need is that integrate the equation and evaluate it as each step of the time. That means if we have 3000-time steps, we are going to integrate 3000 times for each step.
1- integrate
2- evaluate the integration at each step of the time
the result should match the size of t
Yes, so what you have to do is divide the integral interval in small intervals
suppose your time starts from 0 and ends at 10s with 1s second step (so there will ber 11 steps of time)
0 1 2 3 4 5 6 7 8 9 10
Iteration 1:
y=integral(your_function,0,1);
Iteration 2:
y = integral(your_function,1,2);
Iteration 3:
y = integral(your_function,2,3);
.
.
.
Iteration 10:
y = integral(your_function,9,10);
So you have 10 different outputs
Similarly you can do it for 3000 time steps, what you need is current time step and previous time step (feedback).
I am not much familiar with simulink, if it doesnot work, try setting the xmin =0 for every iteration
(integral(fun,xmin,xmax))
Inside the simulation when I use clock as my time I cannot control the time to divide it into periods
Is this for continuous or discrete work?
What is the expected output for these three cases:
  • current time is "close enough" to the end of one of the defined intervals
  • previous time and current time are both in the middle of one of the defined intervals
  • previous time was in the previous defined interval compared to the current time
I am using solver type ( Variable-step ) solver ode45. I am using this to have current time results
With variable-step solvers, the block will be called at irregular intervals that might not match the interval that you want solutions at. The variable-step solvers can also go backwards in time (I encountered a case in which this really happened within the last few days.) ode45() invokes the function 6 times per step, using slightly different times and slightly different boundary conditions in order to make projections and check the error measurement.
In order to get results for a fixed time interval, because the function could be called for times spanning multiple intervals, you would have to emit a variable number of results -- and you would have to be prepared to have the values "revoked" if ode45 decided that the step was a failure. ode45 does not always go backwards in time when it decides that a step is a failure.
What is the reason that you are trying to replace those blocks with a MATLAB Function Block ? Are you trying to move to a discrete time system, using MATLAB Function Block to replace the ode45() work over a known interval?

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Programmatic Model Editing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by