Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can you write a matlab code please, thankyou Now that you can use Matlab to find critical points of a function of two variables, you

image text in transcribedimage text in transcribedimage text in transcribedCan you write a matlab code please, thankyou

Now that you can use Matlab to find critical points of a function of two variables, you are ready to write a function M-file that can take an arbitrary function as input and calculate its critical points! Begin by creating a new M-file, "CritPt.m" with the first line: 1] function [ cr ] = CritPt(f, vari, var2) Your task is to write the rest of the function M-file so that entering the following command in Matlab will reproduce the critical points found in Exercise 2: >> Critpt('x^3+2*y^2-2*x*y','x', 'y') Show/hide note on the Function Input Format Since the input 'x^3+2*y^2-2*x*y' is surrounded by single quotes Matlab will treat it as a text input, or a string of characters. If we don't do this Matlab will try to compute the expression, which would result in an error message! Having the input as text is an easy way to get around this, but now that it's text it can't be used as an equation. We'll have to add a command inside the function M-file that converts it into an equation of symbolic variables. Inside the function M-file the variable f will be used to represent the inputted text expressions such as 'x^3+2*y^2-2*x*y'. To turn these text expressions into algebraic ones you can use the command: f = evalin(symengine, f); Enter this command as the second line of commands in your function M-file. The input variables also need to be converted to symbolic variables, so following this line enter the commands: varl = var2 = sym(vari); sym(var2); Now add lines of commands that calculate the first order partial derivatives of the expression f and store them as the variables fx and fy. Show/hide hint You will need to command. use the diff function in each Add another line of commands to solve for the critical points of the function and store the calculated x and y coordinates in two vectors xcr and ycr. Show/hide hint You will need to use the solve function. This commands should be the same as those used in Exercise 2. Set the output of your function file to be the horizontal concatenation of xcr and ycr. Show/hide hint The output needs to be defined as the variable cr for the function M-file to work. How can you set cr to be the concatenation of xcr and yor? Convert from symbolic variables The results that are output are symbolic variables. This means that it needs to be converted back to a regular double variable using cr = double(cr); Save your M-file and run the command: >> Critpt('x^3+2*y^2-2*x*y', 'x', 'y') The output should correspond to the critical points calculated in Exercise 2. Your function M-file should be independent of the variables used as input in the Command Window. Make Tyne hore variables used as input in the Command Window. Make sure this is the case you haven't used any variables in you M-file other than f, vari, and var2 and then submit your M-file. Show/hide hint You can test this by trying the command: >> CritPt('5^3+2*t^2-2*s*t','s', 't') If your function M-file is independent of the variables used as input then the output should be the same coordinates as when you used the command: >> Critpt('x^3+2*y^2-2*x*y', 'x', 'y') Once you are ready, click "Choose File" and choose your M-file. Afterwards, click "Upload your M-file", which will upload your function script for marking

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Students also viewed these Databases questions