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

this is matlab problem. Introduction Computing the derivatives of functions are

ID: 3732272 • Letter: T

Question

this is matlab problem.

Introduction Computing the derivatives of functions are a critical step in most optimization problems. In this exercise, we will look at the sine wave or sinusoid, which desribes a smooth, repetitive oscillation. Its most basic form, as a function of time, is as follows: f(t) = A sin(t + ) (1) In the equation above, A is referred to as the amplitude, is the angular frequency, and is the phase. In this program, you will implement an animation that draws the tangent line on the sine wave using the derivative for each point in the input domain (the number of elements in the domain vector is specified later in the assignment). You will approximate the derivative and draw the tangent line at each point as an animation. One method to approximate derivatives for arbitrary functions is called finite differences. The basic idea is to look at a point (x, f(x)) and its neighbor to the right (called forward difference) (x + h, f(x + h)) or neighbor to the left (x h, f(x h)) (called backward difference), and approximate the slope using differences as follows: d[f(x)] dx = f(x) x = f(x + h) f(x) (x + h) x (2) You are to compute the derivative (forward) at each point in the domain using a for loop, and draw the tangent line, as in Figure 1, as an animation for all points (except last) on the time domain. The animation should be smooth, as you progress from left to right, the tangent should be horizontal on peaks and troughs because the derivative (or the slope) is zero. Your program has to follow exact specifications as follows: • The program has to prompt the user for A, and . • The domain of the sine function will consist of 500 equally spaced elements between 0 and 4. • You have to use a for loop and iterate through 1:end-1. elements of the domain.

Figure 1: Sample visualization of derivative at a point on the sine wave. On the left: A = 1, = 1, = 0, on the right: A = 2, = 2, = 0. • You have to implement forward differences to compute derivative value at each point . • You will compute the tangent line using the slope and draw it with the plot command as two points, solid red, and x markers. The length of the tangent line is 2 seconds (from current t 1 to t + 1). • You will plot a blue solid dot at the point at which you are currently computing the derivative for using the scatter command. • Your program will label the x-axis as t and y-axis y(t). • Your program will make a legend, exactly like the one given in the example figure above.

Figure title The title of the figures in the examples above contain numbers. The title command takes a “string” argument, which can be thought of as a row vector of characters. For example disp([’AB’, ’CD’]) will display ABCD. The MATLAB function num2str converts a number to string. Take the following code as an example: t = 3.433; dt = 1.034; figure_title = [’The derivative at t=’, num2str(t), ’ is: ’, num2str(dt)]; title(figure_title); The above code will set the current figure title to: “The derivative at t=3.433 is: 1.034”. Use the code above as a guide to set your figure title as shown in the example Figure 1 above. This has to be done for every frame

Derivative at t-4.2559 is: 0.42941 Derivative at t 4.2559 is: -2.5245 sin(t) tangent line point on sin(t) sin(t) _targent line · point on sin(t) 10 12 10 12

Explanation / Answer

clear clc t=linspace(0,4*pi,500); A=input('Enter A:'); w=input('Enter omega: '); ph=input('Enter phase: '); y=@(t)A*sin(w*t+ph); y1=y(t); m=max(y1); dt=t(2)-t(1); figure for i=1:499 p=(y(t(i)+dt)-y(t(i)))/dt; plot(t,y1,'b'); hold on axis([0 4*pi -(m*A+4) m*A+4]) y11=p*((t(i)-1)-t(i))+y(t(i)); y12=p*((t(i)+1)-t(i))+y(t(i)); plot([t(i)-1 t(i)+1],[y11 y12],'r-*') scatter(t(i),y(t(i)),'b','filled') figure_title = ['The derivative at t=', num2str(t(i)), ' is: ', num2str(p)]; title(figure_title) legend('sin(t)','tangent line','point on sin(t)') hold off pause(.1)%change animation speed here, the more seconds you put ere, the slower it gets end