need matlab code and which is in working condition and give

need matlab code and which is in working condition and give right output..

this is very basic

Solution

function [ actual_val,approx,n ] = approx_cos( x,error)
actual_val=cos(x); %Actual value of cos(x)
approx=0;   %Initial approximation is zero
i=0;        %Counter for summation
while(abs(actual_val-approx)>error) %Check if error is within range
    approx=approx+(-1)^i*x^(2*i)/factorial(2*i);    %If not, add new term
    i=i+1; %Add to counter
end

n=i-1;    %Output n as the number of iterations required

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~END OF PROGRAM~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Sample usage:
[a,b,c]=approx_cos(pi/3,0.0001)

a will output the actual value of cos(x)
b will output the approximated value
c will give the number of iterations required

need matlab code and which is in working condition and give right output.. this is very basicSolutionfunction [ actual_val,approx,n ] = approx_cos( x,error) act

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site