Inputting a time varying equation into system in simulink

3 views (last 30 days)
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 Comments
Reed Smith
Reed Smith on 4 Dec 2022
I would like to use the function tau(t) as an input for my system.

Sign in to comment.

Accepted Answer

Sam Chak
Sam Chak on 5 Dec 2022
Edited: Sam Chak on 7 Dec 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 Comments

Sign in to comment.

More Answers (1)

Sara Nadeau
Sara Nadeau on 5 Dec 2022
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 Comments
Sam Chak
Sam Chak on 7 Dec 2022
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! 🙏

Sign in to comment.

Categories

Find more on General Applications in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!