Question

In: Advanced Math

a) Write an function that finds the independent variable value at which a mathematical function is...

a) Write an function that finds the independent variable value at which a mathematical function is maximized over a specified interval. The function must accept a handle to a function, and evaluate that function at values ranging from x1 to x2 with an increment of dx. The value returned is the value of x at which the maximum value of f(x) occurs.

Function syntax: xm = xmax(f,x1,x2,dx);

As was the case in the previous problem, this function does not find the true value of x that maximizes f(x). It only identifies the value of x that yields the maximum value of f(x) from among those values of x at which the function is evaluated. How closely this approximates the true maximum point of f(x) will depend on the step size, dx.
Your function should use only basic arithmetic operations and loop structures. You may not use any built-in MATLAB functions (e.g., mean.m, sum.m, max.m, min.m, etc.).
Next, you will investigate how the estimated maximum point varies as a function of dx.

b) Write an m-file, ENGR112_HW6_2.m, in which you define the following as an anonymous function.

?(?)=−?^2+15??+21.843

Define a logarithmically-spaced vector, dx, of 2000 step sizes, from 10-4… 100, at which xmax.m will be used to approximate the maximizing value of f(x) over the interval of 0 ≤ x ≤ 50. Call xmax.m at each value of dx and build a vector of maximizing independent variable values, xm. In

c) Plot the vector of approximate maximizing values as a function of step size using a logarithmic axis for the step size. You should see that the approximation converges as step size gets small enough.

Solutions

Expert Solution


%%Finding maximum value of an array
clear all
close all

%function for finding maximum f(x) for given x
f=@(x) -x.^2+15.*pi.*x+21.843;

x1=0;
x2=50;
%logarithmically spaced vector of 2000 step

dx=logspace(-4,2,2000);

%finding xm values for different dx
for i=1:length(dx)

     xm(i) = xmax(f,x1,x2,dx(i));
   
end

semilogx(dx,xm)
xlabel('step size dx')
ylabel('xmax values')
title('xmax vs. step size plot')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%function for finding maximum value
function [xm] = xmax(f,x1,x2,dx)
  
    %all x values
    x=x1:dx:x2;
    y=f(x);
  
    %finding maxima for the function
    for i=1:length(x)-1
        if y(i)>=y(i+1)
            t1=y(i);
            y(i)=y(i+1);
            y(i+1)=t1;
          
            t1=x(i);
            x(i)=x(i+1);
            x(i+1)=t1;
        end
    end
  
    xm=x(end);
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

   


Related Solutions

Write a recursive function that finds the minimum value in an ArrayList. Your function signature should...
Write a recursive function that finds the minimum value in an ArrayList. Your function signature should be public static int findMinimum(ArrayList<Integer>) One way to think of finding a minimum recursively is to think “the minimum number is either the last element in the ArrayList, or the minimum value in the rest of the ArrayList”. For example, if you have the ArrayList [1, 3, 2, 567, 23, 45, 9], the minimum value in this ArrayList is either 9 or the minimum...
Write a C function that finds and displays the maximum value ina two-dimensional array of...
Write a C function that finds and displays the maximum value in a two-dimensional array of integers. The array should be declared as a 10-row-by-20-column array of integers in main (), and the starting the address of the array should be passed to the function. Modify the function so that it also displays the rows and columns number of the element with the maximum value
This class will model a class variable to hold a mathematical function of up to 10 terms.
// Complete the implementation of the following class.// This class will model a class variable to hold a mathematical function of up to 10 terms.// The function will have one independent variable.// Terms that the function will model are:// Monomials of the form: 5.3x^0.5// Trigonometric terms sin(x), cos(x), tan(x) of the form: 1.2cos(3.0x)//public class Function { // PLEASE leave your class name as Function   // CLASS VARIABLES     // use any collection of variables as you see fit.     // CONSTRUCTORS   //...
Write in C programming language Write the function replace(char b[], char f[], char t[]). which finds...
Write in C programming language Write the function replace(char b[], char f[], char t[]). which finds the string 'f' in the string 'b' and replaces it with the string 't'. You can assume that f and t same length Example: char string[] = "zap";     replace(string, "ap", "oo"); --changes 'string' to "zoo".     *don't assume substring being replaced is singular, or that its own substrings are unique.     *Don't re scan letters already checked and replaced         char string[] =...
please write in c++ 2. Write a function sumOfArray that recursively finds the sum of a...
please write in c++ 2. Write a function sumOfArray that recursively finds the sum of a one-dimensional array. A sample run is below. The elements of the array are: 0 8 -4 6 7 The sum of the array is: 17 Press any key to continue . . .
For each of the following relationships, state which variable is the independent variable and which is...
For each of the following relationships, state which variable is the independent variable and which is the dependent variable, also state whether you expect the correlation to be positive or negative. The age of manufacturing equipment and the number of rejects produced by the equipment. Average outdoor temp and the number of days a ski hill is open during a Winter. Number of hours worked and total pay received during a two week period. Number of kilometers driven and age...
Which variable should be the dependent variable and which should be the independent variable? Why? Plot...
Which variable should be the dependent variable and which should be the independent variable? Why? Plot the points on the scatterplot graph. NOTE: In a scatterplot the x axis will always reflect 1, 2, 3… not the Quarters specifically. Label both axes with words. In order to analyze the data we will use the line of best fit. Answer with 1-3 complete sentences. Does the line seem to fit the data? Why? Calculate the following use excel trendline: y-intercept to...
Write code that finds the largest number in data and stores it in the variable max...
Write code that finds the largest number in data and stores it in the variable max and the smallest number in data and stores it in min. Test Cases Test case #1 Expected result: max is 52.66; min is 15.53 Test case #2 Expected result: max is 56.95; min is 5.77 Test case #3 Expected result: max is 77.02; min is 24.24 Test case #4 Expected result: max is 90.48; min is 35.94.
For the following variables, identify which is the Independent Variable and the Dependent Variable. Then create...
For the following variables, identify which is the Independent Variable and the Dependent Variable. Then create a hypothesis (be sure to also include the direction of the relationship) and then create a null hypothesis. Temperature and crime rates Hours studied and test scores Number of police officers and crime rate
Python: How would I write a function that takes a URL of a webpage and finds...
Python: How would I write a function that takes a URL of a webpage and finds the first link on the page? The hint given is the function should return a tuple holding two strings: the URL and the link text.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT