Azzera filtri
Azzera filtri

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

1 visualizzazione (ultimi 30 giorni)
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

Risposte (1)

Steven Lord
Steven Lord il 13 Giu 2017
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 Commento
dpb
dpb il 27 Giu 2017
"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...

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by