I have a problem in MATLAB Simulink.

조회 수: 3 (최근 30일)
Shiv Nileshkumar Matliwala
Shiv Nileshkumar Matliwala 2022년 9월 13일
편집: Shiv Nileshkumar Matliwala 2022년 9월 16일
So, I want to build the simulink model with "Matlab funcion" as a block. Which has input and output. Now, I have taken sine wave as an input. so "u = sin wave" here I have used "Sin wave" block. Now in function block i have added function as below,
I have attched screenshot of simulink model.
function y = fcn(u)
t_span = 5
y = u(t<t_span);
Here in order to calculate the value of y, I need time "t". So that I can compare it with "t_span". So, When I try to write parameter into "fcn(u)" as "fcn(u(t))" it creates another input port in function block.
So, My question is how can i use internal parameter "t" of sine wave block as a variable for different equations ??
Thank you.
  댓글 수: 4
Sam Chak
Sam Chak 2022년 9월 13일
@Shiv Nileshkumar Matliwala, Before answering your queries, here is a straightforward request:
  • Please sketch the desired output waveform of the MATLAB Function block.
  • If you know the equation of the desired output, please display it here.
Shiv Nileshkumar Matliwala
Shiv Nileshkumar Matliwala 2022년 9월 14일
ok. So,
function x=equa(u)
init_t=0;
final_t=10;
dt=0.001;
N= (final_t-init_t)/dt;
tspan=linspace(init_t,final_t,N)
x = u(tspan==max(tspan(t>=tspan)));
now, here i have written this code inside the "matlab function" block in simulink. So, now in order to run this function i have given input "u" which is coming from sine wave block. but as you can see to get the "x", I also need "t" so that i can compare it with "tspan".
So, in attchement you can see the model i have made. Now, when i run this model I get the following error.
% Undefined function or variable 't'.
% Function 'MATLAB Function' (#48.134.135), line 8, column 24:
% "t"
% Launch diagnostic report.
% Component:MATLAB Function | Category:Coder error
So, now you know my problem. So, if you have any solution to this "t" then please help me.

댓글을 달려면 로그인하십시오.

채택된 답변

Aditya
Aditya 2022년 9월 16일
편집: Aditya 2022년 9월 16일
Hi,
I understand that you are trying to get the parameters from which sine wave is calculated, particularly time “t”.
The sine wave output is calculated from the following formula:
y = amplitude*sin(frequency*t + phase) + bias;
amplitude, frequency, phase, and bias are all sine wave block parameters that are fixed.
For getting the time “t”, you can use the Clock block as discussed in Display and provide simulation time - Simulink (mathworks.com).
Here is an example of using a MATLAB function block to generate same waveform as a sine wave. You can see that the function uses the internal parameters of the sine wave block. You can have an extra input to your MATLAB function block from the clock block.
function y = fcn(t)
frequency = 1; %Same as the sine wave block parameter
amplitude = 1; %Same as the sine wave block parameter
bias = 0; %Same as the sine wave block parameter
phase = 0; %Same as the sine wave block parameter
y = amplitude*sin(frequency*t + phase) + bias;
end
  댓글 수: 3
Aditya
Aditya 2022년 9월 16일
편집: Aditya 2022년 9월 16일
@Shiv Nileshkumar Matliwala Just a clarification, you don't need to implement Sine wave as a block. You can get "t" directly from the Clock Block
Shiv Nileshkumar Matliwala
Shiv Nileshkumar Matliwala 2022년 9월 16일
편집: Shiv Nileshkumar Matliwala 2022년 9월 16일
Yes. And I have posted one more question if you can solve it, it will be a great help sir.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Sources에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by