Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<iomanip>
- #include<cmath>
- using namespace std;
- float a,b,c;
- float Eqa(float x)
- {
- return (x-sin(x)-.5);
- }
- int main()
- {
- float e=0.0001;
- float x0,x1,x2,f0,f1,f2;
- cout<<"Enter X1=";
- cin>>x1;
- cout<<"Enter X2=";
- cin>>x2;
- do{
- f1=Eqa(x1);
- f2=Eqa(x2);
- x0=x2-(f2*((x2-x1)/(f2-f1)));
- f0=Eqa(x0);
- cout<<"xo="<<x0<<"\tx1="<<x1<<"\tx2="<<setprecision(4)<<x2<<"\tf0="<<setprecision(4)<<f0<<"\tf1="<<setprecision(4)<<f1<<"\tf2="<<setprecision(4)<<f2<<"\n";
- x1=x2;
- x2=x0;
- }while(fabs(f0)>e);
- cout<<"Root:"<<x0;
- }
- jaco
- #include<iostream>
- #include<math.h>
- using namespace std;
- int a[3],b[3],c[3],d[3];
- float fun1(float x2,float x3)
- {
- return (d[0]-b[0]*x2-c[0]*x3)/a[0];
- }
- float fun2(float x1,float x3)
- {
- return (d[1]-a[1]*x1-c[1]*x3)/b[1];
- }
- float fun3(float x1,float x2)
- {
- return (d[2]-a[2]*x1-b[2]*x2)/c[2];
- }
- int main()
- {
- float x1=0,x2=0,x3=0,f1=0,f2=0,f3=0;
- float e=0.0001;
- for(int i=0;i<3;i++)
- {
- cout<<"Enter Equation "<<i+1<<" as ( ax1 + bx2+ cx3 = d)\n";
- cout<<"a=";
- cin>>a[i];
- cout<<"b=";
- cin>>b[i];
- cout<<"c=";
- cin>>c[i];
- cout<<"d=";
- cin>>d[i];
- }
- cout<<"F1 \tF2 \tF3"<<endl;
- do
- {
- x1=f1;
- x2=f2;
- x3=f3;
- f1=fun1(x2,x3);
- f2=fun2(x1,x3);
- f3=fun3(x1,x2);
- cout<<f1<<"\t"<<f2<<"\t"<<f3<<endl;
- }while(fabs((f1-x1)/f1)>e &&fabs((f2-x2)/f2)>e &&fabs((f3-x3)/f3)>e );
- cout<<endl<<"Ans: F1 "<<f1<<"\t F2 "<<f2<<"\tF3 "<<f3;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement