Inputting a time varying equation into system in simulink

I am unsure of how to input a time varying equation into my system in simulink.
For example I want to input the function tau(t) = 0t for all t.
This gives me the option to change to a different time varying function instead of setting it to constant 0. However, I am unsure of how to do this because if i set my tau variable in workspace to 0, and try to input from workspace into my system it gives me an error message saying it must be a 2D matrix to account for the time values.
However, I do not know how to make my tau function into a matrix. I thought of using timeout variable however my simulink system does not run so I cannot use that variable.
Any help is appreciated as I am an absolute beginner when it comes to Simulink.

2 Kommentare

Do you want to insert the function tau(t), or to solve an implicit function tau(t) = 0 for t?
I would like to use the function tau(t) as an input for my system.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Sam Chak
Sam Chak am 5 Dez. 2022
Bearbeitet: Sam Chak am 7 Dez. 2022
Edit #2: As suggested in my original Answer, you can use the Clock to generated the time t vector, so that you can describe the time-based function tau as you wish. Here are 3 approaches:
Edit #1: There are a few options to compute the desired torque to stabilize the pendulum bob at the inverted position:
where the moment of inertia is .
You can construct one of these equations in Simulink using the basic blocks the same way as you showed in your image. The following example is coded in MATLAB instead of Simulink. Expect to get tghe same results in Simulink.
g = 10; % gravity
m = 0.4; % pendulum mass
l = 0.1; % pendulum length
params = [g m l]; % stored parameters
tspan = 0:0.01:3;
x0 = [-pi 0];
[t, x] = ode45(@(t, x) odefcn(t, x, params), tspan, x0);
plot(t, x(:,1), 'linewidth', 1.5), grid on
xlabel({'$t$'}, 'interpreter', 'latex')
ylabel({'$\theta(t)$'}, 'interpreter', 'latex')
title('Response of Inverted Pendulum')
% Inverted Pendulum
function xdot = odefcn(t, x, params)
xdot = zeros(2, 1);
g = params(1); % gravity
m = params(2); % pendulum mass
l = params(3); % pendulum length
Ix = m*(l/2)^2; % pendulum inertia
% Full 360° Computed Torque
tau1 = Ix*(- (g/l)*sin(x(1)) - (g/l)*x(1) - 2*sqrt(g/l)*x(2)); % Option #1
tau2 = Ix*(- 2*(g/l)*sin(x(1)/2) - (g/l)*tanh(x(1)) - 2*sqrt(g/l)*x(2)); % Option #2
% Non-360° Computed Torque
tau3 = Ix*(- 2*(g/l)*sin(x(1)) - 2*sqrt(g/l)*x(2)); % Option #3
% Dynamics
xdot(1) = x(2);
xdot(2) = (g/l)*sin(x(1)) + (1/Ix)*tau2; % select one of tau options
end

4 Kommentare

Hi Sam, this is a great answer, thank you! I will look into your code as it looks like it could be very helpful for me in the future!
However, I am not looking to compute the desired torque. The torque in this simulation is set to 0 for all time. I wanted to input the torque as a function of time to allow for future changes to the torque input equation. For instance, if i wanted to change the torque to 2t for all t.
This is what my problem is, as I am totally new to simulink I cannot figure out which 'blocks' to use to perform this. As shown in my screenshot, I used a 'from workspace' block to input my function Tau. However, for this to work, my variable tau has to be defined for all time. I am unsure how to do this.
I have made a 2nd edit to show you 3 ways you can input the time-based function for tau.
The MATLAB code in the 1st edit is kept there for your future reference.
If you are satisfied with these approaches in Simulink, please consider accepting ✔ and voting 👍 the Answer.
This is an awesome answer, seriously, thank you.
Don't mention it @Reed Smith. I'm glad for the opportunity to help you.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Sara Nadeau
Sara Nadeau am 5 Dez. 2022

0 Stimmen

My question is whether the function tau is actually part of the system you are trying to model or whether it's just that the result of the function tau represents the input to the system you are trying to model. I am not sure how to interpret wanting to "use the function tau(t) as input for my system". I can help you use the result or output from tau(t) as input for your system, though.
If you're just using tau to generate the input signal you need for your system, I have some additional questions so I can try to help you figure out how to load the data:
  1. It looks like you are passing a time value to tau to generate the data you're trying to load. Do you have access to this time data to use in constructing the input signal? For most input loading formats, you do need to provide the time data.
  2. Are these values evenly spaced in time? Are you using a uniform sample rate or step size to generate the data? If not, you must provide time values to accurately depict the input data.
This page provides simple code examples to create data in each format the From Workspace block supports: Load Data Using the From Workspace Block. These formats are also supported by other loading blocks, including root-level Inport blocks.

4 Kommentare

Sorry if my question is not clear, I am new to simulink and I don't fully understand how to articulate my problem very well.
I have been given the task of modelling an inverted pendulum system in simulink. I have set up everything else correctly, however, I am struggling to understand how to model my input torque of tau.
In this case the input torque, tau, is 0 for all time, t ∈ [0, 5]. But we have been tasked by modelling this as a function of time not just constant 0 so we can easily modify this input.
I do not have the time data, could i generate this by linspace?
Sam Chak
Sam Chak am 6 Dez. 2022
Bearbeitet: Sam Chak am 6 Dez. 2022
I see... is actually the Input Torque signal to the Inverted Pendulum.
It is not given for free. It is something to be "designed" so that the Pendulum bob maintains at the upward position.
Can you show the differential equation (also known as Equations of Motion) of the Inverted Pendulum?
Does it look like this
or this
?
Thank you, Sam, for your help. Sorry for the late responses!
It looks like this:
Here is a screenshot of my simulink setup. The only bit I am struggling with is inputting τ.
Hope this makes more sense. I feel as though I have completed the hard part and yet I'm clueless on inputting a time based signal.
You're welcome @Reed Smith. A few design options for tau, are suggested in my edited Answer above. Select one of the tau equations and construct it using basic blocks in Simulink, like you did above.
If you find the tau equations and MATLAB code helpful, please consider accepting ✔ and voting 👍 the Answer. Thanks a bunch! 🙏

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Programming finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by