The function f x should be written using element-wise operators, e. Preassigned parameters can be substituted in the 'function' expression in the same way as for the fzero command. The result, which obviously cannot be a fraction, should be rounded to the nearest lesser integer. The next command should be typed and entered from the Command Window:. Following these definitions, the derivative can be presented for numerical calculations as:.
Thus, the derivative can be calculated at each i -point as the ratio of y - and x -differences between neighboring points. The diff command for these differences has the forms:. For example, a species concentration c changes with time t due to a chemical reaction, as shown here:. The commands to be applied in the Command Window are:. The second-order derivative, representing the acceleration rate of the reaction, ,.
Negative values of a indicate deceleration of the reaction. The same results can be reached via the second-order c differences:.
Note, that if the step h of argument t is constant, it can be used in changing diff t. The derivatives of a function given by an equation can be determined in the same way as tabulated data; the step in t in such a case can be smaller and lead to more exact derivative values. Significant deviations in the values of r and t are evidence that the number of points in the table is not enough for exact determination of the derivatives.
They also confirm that for an analytically given function, more points apparently provide more exact results. Frequently, a problem involving a derivative is formulated so that the latter reaction rate, population growth, etc. In such a case the minimal range of t or any other possible argument — x , z , etc.
The process of matching an expression or curve to the data points is called curve fitting or regression analysis. The fitting expression may be based on a theory or chosen empirically.
The process consists of finding the values of the coefficients of the mathematical expression which should fit the experimental data. In the case of a polynomial expression. When the experimental data contain n points x , y the n coefficients can be determined by solving the set of y x equations written for each of the points.
Polynomial fitting where the number of coefficients is the same as that of points is not always efficacious and might lead to significant discrepancies in regions between data points. Another case of possible inefficiency is a linear y x dependence where two points suffice for determination but the experimental data contain a larger number, which means more equations than unknowns.
The commonly used method for better fit is least squares, in which the coefficients are determined by minimizing the sum of squares of the differences between the polynomial and data values; these differences are called residuals and are denoted R. The minimization is done by taking the partial derivative of R with respect to each coefficient and imposing their equality to zero, for example for the straight line and and the set of two equations should be solved to determine a 1 and a 2.
With the fitting coefficients the y -values can be calculated at any x in the fitted interval with the polyval command; this command has the following simplest form.
For example, the height of five seedlings and the amount of nutrients for each of them are 4. After typing and entering the file name in the Command Window, the following coefficients are displayed and the graph in Figure 4. Many bio-applications require an exponential or a logarithmic fitting function:. In the second case the polyfit function can be used with argument log x or log10 x instead of x , e.
Body mass index BMI I is a measure that relates the body weight G in kg and height h in m; it is defined as. With the script file name BMI entered into the Command Window, the program displays the prompt to input the weight in kg; with the weight typed and the Enter key pressed, a new prompt for height in m appears; with the latter typed and entered, the BMI is calculated and displayed together with a conclusion. Your BMI is For women this index is given by the expression:.
Problem : Write a script program for calculating and displaying the BMR. The script file solving this problem is written below. After entering into the Command Window the script file name BMR, the program displays the prompt to sex, type 1 or 2 male or female.
After pressing the enter key, prompts then appear successively about weight in kg, height in m and finally age. The BMR is then calculated and displayed. The concentration of components in solutions is expressed in different forms, among them molarity, molality and molar fraction. The expression for conversion from M to m and to x is. Problem : Write a function file with name molfrac that calculates and displays solute molality and molar fraction for a given solute molarity and molar mass, and solution density.
The function file is:. Problem : Write a function file with name europes that predicts and displays the population in Use for prediction the interp1 function with three extrapolation alternatives provided: 1, linear; 2, cubic; 3, spline. The first line of the function should be the function definition line with the function name europes and the input and output parameters.
Thus, the following input parameters are specified: yi , the target year, and num , the chosen method alternative: 1, linear ; 2, cubic ; 3, any other number except 1 and 2 for the spline.
The specified output parameter is pei — the predicted population. The function file written to solve the problem is:. The following appears in the Command Window when the file was run:. Problem : Write a function file reactime. The input parameters for the function definition line are: the concentrations [ B ] and [ A ] 0 , the reaction rate constants k 1 and k 2 , and the initial value x 0 ; the output parameter is time t.
In using fzero , the variable t in the expression above should be named x. The function file for solving this problem is:. The amount m of the compound after time t is:.
Problem : Write a script that calculates the amount of the compound every 30 minutes over 3 hours and plot the graph m t. Species dissociation is described by the equation. The program is realized in the form of a function named DissRate with the following input arguments: m0, the species amount at starting time; k, the reaction constant; tstart , the starting time; tend , the end time; and h, the time step. After running this file, the following appear in the Command and Figure windows:.
A high level of a particular substance in the atmosphere may increase the incidence of cancer. The declaration includes the function keyword, the names of input and output arguments, and the name of the function. After you save the file, you can call the function with different base and height values from the command line without modifying the script:.
Functions have their own workspace, separate from the base workspace. Therefore, none of the calls to the function triarea overwrite the value of a in the base workspace.
Instead, the function assigns the results to variables a1 , a2 , and a3. Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select:. Select the China site in Chinese or English for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Phinite Academy. Previous Page. Next Page. Live Demo. Useful Video Courses. More Detail. Previous Page Print Page. Save Close.
0コメント