Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem 1: Write a MATLAB function that minimizes f(r) (r-1) sin r subject to a

ID: 668460 • Letter: P

Question

Problem 1: Write a MATLAB function that minimizes f(r) (r-1) sin r subject to a a s b, where a and b are user input; your MATLAB function should be called "y ournameMinimizef.m and its first line should be function Csolution,optimalobjfunct] yournameMinimizef (a,b,h) and the output "solution" should be the global minimum of f constrained to z a S z S b, and optimalobjfunct" should be the optimal objective function value. The way your function should solve this is by brute force; check all of the values a, a h, a +2h,a +3h, a 4h, until you get to b, and compare them, and output the best one of these. Hand in the following: a) A copy of the file yourname b) A diary in which you execute the following commands: solution, optimalobjfunct] yournameMinimizef (-3,3, .001) solution, optimalobjfunct] yournameMinimizef 1,3 001) solution, optimalobjfunct] yournameMinimizef (.1,3, .001) solution, optimalobjfunct] your nameMinimizef (.1,4,.001)

Explanation / Answer

Ans )

CODE -
       function [solution,optimalobjfunct] = yournameMinimizef(a,b,h)
           grid=[a:h:b];
           f=(grid-1).^2.*sin(grid);
           [optimalobjfunct.position] = min(f);
           solution = grid(position);
           plot(grid,f,'r.')

Commands' result          
       a - [solution,optimalobjfunct] = yournameMinimizef(-3,3,.001)
           solution = -2.1380
           optimalobjfunct = -.1208
          
       b - [solution,optimalobjfunct] = yournameMinimizef(-.1,3,.001)
           solution = -.1000
           optimalobjfunct = -.1208
          
       c - [solution,optimalobjfunct] = yournameMinimizef(.1,3,.001)
           solution = 1
           optimalobjfunct = 0
          
       d - [solution,optimalobjfunct] = yournameMinimizef(.1,4,.001)
           solution = 4
           optimalobjfunct = -6.8112