matlab ode45 wont accept new parameter
2 次查看(过去 30 天)
显示 更早的评论
% Set extra parameters
tf = 72; % h
h0 = 2; % m
g = 9.81;
%prompt = 'What is the leakage value? ';
%a = input(prompt);
%L = a*g*h;
% Solve the DAE
opts = odeset('Reltol',1e-5);
[t,h] = ode45(@tankf1,[0 tf],h0,opts);
% Plot the graph
plot(t,h)
title('Tank model solved by Method 1')
xlabel('Time (h)')
ylabel('Level (m)')
grid on
function P1 = P1function(t)
% P1FUNCTION: Upstream pressure (kPa) as a function of time (h)
if t<=10
P1 = 139.5;
elseif t<=11
P1 = 139.5 + 60.5*(t-10);
elseif t<=40
P1 = 200;
elseif t<=55
P1 = 200 - 30*(t-40)/15;
else % t>55
P1 = 170;
end
end
function dhdt = tankf1(t,h)
% TANKF1: Tank level model - Derivative function for solution method 1
% (substitution of algebraic variables)
% Tank model parameters and constants
A = 12.5; % m^2
CV1 = 3.41; % (m^3/h)/kPa^0.5
CV2 = 3.41; % (m^3/h)/kPa^0.5
P0 = 100; % kPa
P3 = 100; % kPa
rho = 1000; % kg/m^3
g = 9.81; % m/s^2
P1 = P1function(t); % kPa
% Algebraic equations for F1, F2 and P2 have been substituted into the
% differential equation for dh/dt to create a pure ODE system:
dhdt = 1/A*(CV1*sqrt(P1-P0-rho*g*h/1000) - CV2*sqrt(P0+rho*g*h/1000-P3))-L;
end
I have the code above shown, and it is a simulation of a tank with water levels at a certain time, up to 72 hours. The issue im having is I need to simualte it with a leakage value, this is calculated as shown at the top of code using a*g*h. The issue im having it when I try to put it in the dhdt equation in the function for tankf1, it keeps throwing an ode error and cant figure out how to alter this. NB I have to use the ode to solve the equation and draw the graph.
0 个评论
回答(1 个)
madhan ravi
2018-12-5
编辑:madhan ravi
2018-12-5
EDITED
If you want to pass a new parameter to a function parametrize your function
% Set extra parameters
tf = 72; % h
h0 = 2; % m
g = 9.81;
a=input('leakage value : ');
L=a*g*h;
%prompt = 'What is the leakage value? ';
%a = input(prompt);
%L = a*g*h;
% Solve the DAE
opts = odeset('Reltol',1e-5);
[t,h] = ode45(@(t,h)tankf1(t,h,L),[0 tf],h0,opts);
% Plot the graph
plot(t,h)
title('Tank model solved by Method 1')
xlabel('Time (h)')
ylabel('Level (m)')
grid on
function dhdt = tankf1(t,h,L)
% TANKF1: Tank level model - Derivative function for solution method 1
% (substitution of algebraic variables)
% Tank model parameters and constants
A = 12.5; % m^2
CV1 = 3.41; % (m^3/h)/kPa^0.5
CV2 = 3.41; % (m^3/h)/kPa^0.5
P0 = 100; % kPa
P3 = 100; % kPa
rho = 1000; % kg/m^3
g = 9.81; % m/s^2
P1 = P1function(t); % kPa
% Algebraic equations for F1, F2 and P2 have been substituted into the
% differential equation for dh/dt to create a pure ODE system:
dhdt = 1/A*(CV1*sqrt(P1-P0-rho*g*h/1000) - CV2*sqrt(P0+rho*g*h/1000-P3))-L;
end
function P1 = P1function(t) % should be defined at last
% P1FUNCTION: Upstream pressure (kPa) as a function of time (h)
if t<=10
P1 = 139.5;
elseif t<=11
P1 = 139.5 + 60.5*(t-10);
elseif t<=40
P1 = 200;
elseif t<=55
P1 = 200 - 30*(t-40)/15;
else % t>55
P1 = 170;
end
end
example of the graph produced:
11 个评论
Steven Lord
2018-12-5
Mitul,
When you wrote this:
input = 'What is the leakage value? ';
a = input(input);
L=a*g*h; % change L to your value
there is one problem with the code and one potential problem with the workflow.
Problem with the code: because you've defined a variable named input, you cannot call the input function. Rename the variable input to something like promptString.
Potential problem with the workflow: as written each and every time ode45 calls your ODE function tankf1 it will prompt the user to enter a value. Since the ODE solver will call your ODE function many times, this seems inefficient. Unless the leakage value changes over time in a way that's not computable from the previous leakage value and the current time, I'd prompt the user once for the value before you call ode45 and pass that value into your ODE function as a parameter as madhan demonstrated.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!