Task #1 Write a single function script to perform the Fourth Order…
- User
Task #1 Write a single function script to perform the Fourth Order Adams-Bashforth-Moulton algorithm on a system of ordinary differential equations (IVP). The program must be able to handle any number of differential equations (an IVP with any number of state variables). Your function m-file will receive through its header the following problem parameters (in this exact order): a. a cell array containing the function handles of the equations describing the time derivatives of the several state variables b. the initial augmented state-array – a numerical row array containing the needed starting values of the independent variable and the several state variables (in the same order as the function handles in the cell array from the first input argument) c. the step size to be used, and d. the number of steps to take
Function Output Your file should not return anything to the calling program. Instead, it should i. print to screen (the command window) the final state of the system, including t, and ii. plot a trace for each of the dependent variables as a function of time. Plot each trace on a separate graph. To facilitate plotting, store the results of all time steps in the augmented state-array a. each row represents the system at a different point in time b. column 1 contains the time, and each other column contains the temporal history of a particular state variable Store only the final result of each time step, not the predicted or non-converged corrected values. Title each plot ‘Var1’, ‘Var2’, etc. Try help plot and help title in the MATLAB command window to learn more about plotting. num2str will also be useful for creating the labels themselves.
Hints for your Function Script Handling a System of Un-predetermined Size Technical Because you will not know in advance the order of the IVP your function will be asked to solve, some of the system parameters will need to be passed in as arrays, rather than as scalars. The step size and number of steps will still be scalars, but there will be an un-predetermined number of state variables, each of which will have an associated starting value and derivative expression. The starting value of each state variable is a scalar, so the collection of them can be stored in a normal array. On the other hand, your function script must be passed a separate function handle for the equation for the time derivative of each state variable. To store function handles, we must use a cell array. So then, you must be able to evaluate a function handle stored in a cell array. We did this in Lab #4, but I cover it again later in this document. NOTE: Because your augmented state array will include time as the first value, the function stored in the nth index of the cell array is actually the derivative of the state variable in the (n+1)th index of the augmented state array.
Function handles with unknown number of inputs If you remember doing this for Modified Secant on a system of equations, then you may skip this section. Up to now, you’ve probably only used function handles for equations that had a predetermined number of inputs. For this program, each derivative equation may be a function of the independent variable and any or all of the state variables. This is only a problem because you don’t know now (as you’re writing the function script) how many or which of the state variables will appear in each of the derivative equations. Fortunately, MATLAB allows us to put arrays into the equation function evaluation. For example, if I define a function handle: d1_by_dt = @(t,a) a(1) * a(3) / (2 + t); when you ask MATLAB to evaluate the mathematical expression, MATLAB will expect to receive a scalar as the 1st argument, and an array of at least three terms as the 2nd argument. It will use the first and third terms in the array, along with the scalar, to evaluate the expression. The augmented state array passed into the function already has t as the first value, so let’s just stick with that. Then our example equation becomes: d1_by_dt = @(a) a(2) * a(4) / (2 + a(1)); You, of course, will have no prior knowledge of which state variables are used in each derivative expression. Therefore, for each derivative evaluation, you will need to submit an array containing the correct values of the dependent variable and all of the state variables, in the same order as the initial values in the passed-in augmented state array. When grading your code, it will be my responsibility to ensure that all of the derivative equations use the correct variables.
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.