Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

1/5/24, 2:46 PM Loading..

<< Search more Solutions!

Found Errors in Solution? >> Report here!

Best Answer

#include<stdio.h>
#include<math.h>
#define ERROR 0.0001//Giving allowed error
//given f(x) function
//f(x) = sin(x)+3cos(x)-2
float f(float x)
{
x=x*(3.142/180);//converting degrees to radians
return sin(x)+3*cos(x)-2;
}
//Main method
int main()
{
//given initial approximations
float x0=2,x1=1.5;
float f0,f1,x2,t;
printf("___________________________________________________________________________");
printf("\nx0\t\tx1\t\tx2\t\tf(x0)\t\tf(x1)\n");
printf("___________________________________________________________________________");
do{
f0=f(x0);//finding f(x0)
f1=f(x1);//finding f(x1)
x2=x1-((f1*(x1-x0))/(f1-f0));//finding x2 value by this formula
printf("\n%f %f %f %f %f\n",x0,x1,x2,f0,f1);
x0=x1;
x1=x2;
if(f1<0)
t=fabs(f1);
else
t=f1;
}while(t>ERROR);
printf("___________________________________________________________________________");
printf("\n\nroot = %f",x2);//final root value
}

https://exedustack.nl/the-pilot-shortage-may-be-easing-slightly-but-aviation-now-needs-mechanics/ 1/4
1/5/24, 2:46 PM Loading..

Likes: 0 Dislikes: 0

Answer 1

Screenshot of the code:

https://exedustack.nl/the-pilot-shortage-may-be-easing-slightly-but-aviation-now-needs-mechanics/ 2/4
1/5/24, 2:46 PM Loading..

Code to copy:

%function to calculate secant value


function [x,a] = secanttest2(f, x1, x2,in, count)

%use f1 and f2 functions for x1 and x2


f1=f(x1);
f2=f(x2);

https://exedustack.nl/the-pilot-shortage-may-be-easing-slightly-but-aviation-now-needs-mechanics/ 3/4
1/5/24, 2:46 PM Loading..

%plot the function sin x + 3 cos x -2


f=inline('sin(x)+3.*cos(x)-2')

%start for loop from 1 to count-1


for a=1:count-1
%calculate x
x = x2 - (f2*(x2-x1)/(f2-f1));
%plot fx
fx=f(x);

%equate and plot the functions


x1=x2;
f1=f2;
fun=abs(x2-x);
x2=x;f2=fx;

%print the values


fprintf('a = %g, x = %g, fx = %g\n',a,x,fx)

%check for the conditions and break


if fun<in, break,
end
end
Likes: 0 Dislikes: 0

https://exedustack.nl/the-pilot-shortage-may-be-easing-slightly-but-aviation-now-needs-mechanics/ 4/4

You might also like