I already wrote my code, I just need help figuring out a couple of errors.
So my function has 3 parts. For the first part, I want to crete a function that finds the derivative of ln x. I want to set x = 2:0.05:4. In the function MyDierentiation, I want to compute the approximated values of derivatives at x by using the MATLAB function diff, and assign the value to df1
function [df1, df2] = MyDifferentiation()
%%Part A
x = 2:0.05:4y= log(x)df1= diff(x)
I am getting an error in my code. My instructor told me that if f'(x) is a 1×21 array, df1 will be a 1×20 array. So, i'm guessing I have to make df1 have the same length as x by assuming that the last point has the same derivative as the preceding point? Is that right? how would I go about doing this on MATLAB?
%%Part B
x = 2:0.05:4y=log(x)df2= polyfit(x,y)df2=polyder(x)
I also compute the approximated derivative using the function polyder (By using the same x = 2:0.05:4 and computing the corresponding y values of the function y = ln(x).) I also wan to fit the x and y data by a 3rd order polynomial. The values of the derivative at x will be assigned to df2.
%%Part C
hold onplot (df1) plot (df2)hold offylabel('derivative')xlabel('x')legend('diff','polyder','exact values')lines = {'diff','polyder','exact values'}legend(lines)
I haven't tested this code because of the errors above, but hopefully it's plot the right graph once the errors are fixed. ( I wanted to plot df1, df2, and the exact values of the derivative in a single plot. Please note that I am using a blue solid line for df1, red dashed line for df2, and green dotted line for exact values. I am also setting LineWidth to 2 and providing a legend to the graph. )
thank you everyone!
Best Answer