Clear Filters
Clear Filters

Is it possible to create a Simulink model from my code?

3 views (last 30 days)
I am new to Matlab and Simulink and i just created some code in Matlab and i tried to do the same thing in Simulink but i was not able to complete it. I was not able to reproduce the system by only using Simulink blocks because i did not find any possibility to get access to the data for the temperatures in previous timesteps. I also tried to use the Simulink function block but it did not work as well because i was not able to get an vector as output.
clear all
% Constants
roh_w = 983.2;
cp_w = 4183;
lambda_w = 0.6544;
dt = 2.5;
m_strom = 1;
r = 0.5;
h = 2.5;
Tanklayers = 60;
Timesteps = 1440;
T_Start = 80;
T_in = 50;
A_Tank = pi*r^2;
h_layer = h/Tanklayers;
v_w = m_strom/(roh_w*A_Tank);
CFL = (v_w*dt)/h_layer;
Fo = (lambda_w*dt)/(roh_w*cp_w*h_layer^2);
T = zeros(Tanklayers,1);
T_store = zeros(Tanklayers,Timesteps);
% Calculating the temperature in the tank
for x = 1 : Timesteps
T_old = T;
if x == 1
for y = 1 : Tanklayers
T(y) = 80;
end
else
for y = 1 : Tanklayers
if y == 1
T(y) = (Fo + CFL)*T_in + (1 - 2*Fo - CFL)*T_old(y) + Fo*T_old(y+1);
elseif y == Tanklayers
T(y) = (Fo + CFL)*T_old(y-1) + (1 - 2*Fo - CFL)*T_old(y) + Fo*T_old(y);
else
T(y) = (Fo + CFL)*T_old(y-1) + (1 - 2*Fo - CFL)*T_old(y) + Fo*T_old(y+1);
end
end
T_store(:,x) = T;
end
end
figure
plot(T_store(50,:))
hold on
ylabel("Temperatur [°C]")
xlabel("Timestep")

Accepted Answer

Sarthak
Sarthak on 21 Feb 2023
Hi Lars,
As mentioned, you tried using the function block but could not get a vector as an output. I believe you have not made the appropriate changes to your function code. The MATLAB function should have the correct input and output variable with the correct return type. If you return a vector, you will get a vector as an output. You can use the DEMUX block to segregate the variables.

More Answers (0)

Categories

Find more on Simulink Functions in Help Center and File Exchange

Tags

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!