## Combining a function and solver to create a M-function in Simulink

Wa3id

### Wa3id (view profile)

さんによって質問されました 2019 年 11 月 14 日 13:01

### Wa3id (view profile)

さんによって 回答されました 2019 年 11 月 18 日 10:05
Wa3id

### Wa3id (view profile)

さんの 回答が採用されました
If I have the following function and solver script:
The function is:
function dydt = odefcn(t,y,A,B)
dydt = zeros(2,1);
dydt(1) = y(2);
dydt(2) = (A/B)*t.*y(1);
The solver script:
A = 1;
B = 2;
tspan = [0 5];
y0 = [0 0.01];
[t,y] = ode45(@(t,y) odefcn(t,y,A,B), tspan, y0);
How can I combine both to create a "MATLAB function" block in Simulink that takes A and B as input, and outputs y?
Thanks a lot!

#### 0 件のコメント

サインイン to comment.

## 2 件の回答

### Wa3id (view profile)

2019 年 11 月 18 日 10:05
採用された回答

For the benefit of those who may have the same question, you cannot use a MATLAB function, but you can use S-Function.

#### 0 件のコメント

サインイン to comment.

### Fangjun Jiang (view profile)

2019 年 11 月 14 日 13:27

Simulink is the perfect tool to solve this problem (get y output based on input A and B). Why take the awkward long route? In fact, I don't think it is even possilbe if it is required to solve it with a MATLAB Function block in Simulink.

Wa3id

### Wa3id (view profile)

2019 年 11 月 14 日 13:38
The thing is that I have a much more complicated functions that's already written as a MATLAB function, and re-creating the function in Simulink from scratch will require signifcant time.
So, I wanted to know how can I solve this kind of problem on Simulink with the functions that I already have.
Fangjun Jiang

### Fangjun Jiang (view profile)

2019 年 11 月 14 日 14:02
When a MATLAB Function block is simulated in a Simulink model, it runs at every time step. Yet you need to solve a problem that is time related. At time t=1, the MATLAB Function block won't know or have access to info/value at time=5. That is why I think it is impossible.
Wa3id

### Wa3id (view profile)

2019 年 11 月 18 日 10:00
Thanks a lot Fangjun. That makes sense

サインイン to comment.

Translated by