Question
Complete using Matlab 4. Use the first derivative functions to estimate the dy/dx value for y=sin(x) at x=pi/3. Use a step size (delta x) of
Complete using Matlab
4. Use the first derivative functions to estimate the dy/dx value for y=sin(x) at x=pi/3. Use a step size (delta x) of pi/10. Compute the magnitude of the error for each approximation (backward, forward, centered).
Use functions provided (can be copied and pasted into matlab)
(forward and Backward apporximation)
function [ firstderivative ] = firstdfrontback( x,y ) % this function finds the first derivative using the forward and backward % method and saves a matrix with three columns, the firsst being th x % values, the second containing the forward apporximation, and the third % containing the bacwards apporximation orig_length=length(x); firstderivative = zeros(length(y),3); if(length(x)> length(y)) orig_length = length(y); firstderivative = zeros(length(x),3); end for i=1:orig_length-1 h=x(i+1)-x(i); firstderivative(i,1)=x(i); firstderivative(i,2)=(y(i+1)-y(i))/h; end for i=orig_length:length(x) firstderivative(i,1)=x(i); firstderivative(i,2)=NaN; end for i=orig_length+1:length(y) firstderivative(i,1)=NaN; firstderivative(i,2)=NaN; end firstderivative(1,3)=NaN; for i=2:orig_length h=x(i)-x(i-1); firstderivative(i,3)=(y(i)-y(i-1))/h;
end for i=orig_length+1:length(x) firstderivative(i,1)=x(i); firstderivative(i,3)=NaN; end for i=orig_length+1:length(y) firstderivative(i,1)=NaN; firstderivative(i,3)=NaN; end
firstderivative end
other function: using center difference
function [firstdiff]= firstcentdiff( x,y ) %this function will find the first derivative using the center difference %method l=length(x); for n=1:l-1 func(n,1)=( x(n+1)+x(n))/2; %%% mid point value func(n,2)=(y(n+1)-y(n))/(2*(x(n+1)-x(n))); %%% first derivatives using cetral difference end firstdiff=func
end
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started