I need to have a user input for a certain number of vectors to be created.

1 次查看(过去 30 天)
I have a vector of ODEs (ordinary differential equations) that I use to solve with the function ode45. My task is to have a user input of 'n' that calls for 'n' number of ODEs in my code. I know how to have the user prompt for 'n', I don't know how to make the program recognize that it needs to create 'n' equations in the function file. Any thoughts?
Right now it's hard-coded to 20 equations, but I need to have it to where user input determines number of equations. There is a recognizable pattern though.
Code for reference:
function [ DeltaO2Concentration ] = O2Solver_beta(t,O2Conc,c,O2Leak)
%UNTITLED Test function used to solve 'first attempt' code.
% System of ODE diffusion equations that will be solved using 'ode45'
% function. Inputs are constant composed of universal and calculated
% constants, time (independent variable), O2Con (dependent variable),
% O2Leak which is a separate term or something.
%VECTOR LENGTH WILL NEED TO VARY ACCORDING TO USER INPUT%
DeltaO2Concentration = [0;
c*t*(O2Conc(1)-O2Conc(2)+O2Conc(3)-O2Conc(2));
c*t*(O2Conc(2)-O2Conc(3)+O2Conc(4)-O2Conc(3));
c*t*(O2Conc(3)-O2Conc(4)+O2Conc(5)-O2Conc(4));
c*t*(O2Conc(4)-O2Conc(5)+O2Conc(6)-O2Conc(5));
c*t*(O2Conc(5)-O2Conc(6)+O2Conc(7)-O2Conc(6));
c*t*(O2Conc(6)-O2Conc(7)+O2Conc(8)-O2Conc(7))+O2Leak;
c*t*(O2Conc(7)-O2Conc(8)+O2Conc(9)-O2Conc(8))+O2Leak;
c*t*(O2Conc(8)-O2Conc(9)+O2Conc(10)-O2Conc(9))+O2Leak;
c*t*(O2Conc(9)-O2Conc(10)+O2Conc(11)-O2Conc(10));
c*t*(O2Conc(10)-O2Conc(11)+O2Conc(12)-O2Conc(11));
c*t*(O2Conc(11)-O2Conc(12)+O2Conc(13)-O2Conc(12));
c*t*(O2Conc(12)-O2Conc(13)+O2Conc(14)-O2Conc(13))+O2Leak;
c*t*(O2Conc(13)-O2Conc(14)+O2Conc(15)-O2Conc(14));
c*t*(O2Conc(14)-O2Conc(15)+O2Conc(16)-O2Conc(15))+O2Leak;
c*t*(O2Conc(15)-O2Conc(16)+O2Conc(17)-O2Conc(16));
c*t*(O2Conc(16)-O2Conc(17)+O2Conc(18)-O2Conc(17));
c*t*(O2Conc(17)-O2Conc(18)+O2Conc(19)-O2Conc(18));
c*t*(O2Conc(18)-O2Conc(19)+O2Conc(20)-O2Conc(19));
0];
end

回答(1 个)

Steven Lord
Steven Lord 2017-6-13
There's a recognizable pattern to the portions of your equations that involve the variable O2Conc. For that piece you probably could use the matrix generated by example 2 on the documentation page for the spdiags function. I don't see a pattern in which ODEs involve O2Leak and which don't.
  1 个评论
dpb
dpb 2017-6-27
"I don't see a pattern in which ODEs involve O2Leak"
I didn't either, but seems as though simply augmenting the array with
O2Conc(sltnodes) = O2Conc(sltnodes) + O2leak;
should fixup the overall as he's passed the vector of locations. The question I've still got is just how many actual terms are supposed to be here...

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by