Question

Write a program to print graph of a function on text terminal. Note that the function is given in the code. Therefore you are

A typical execution and output is given as follows. Inputs are underlined. a) Compile and run your code for given five functi

[user123@ssh]$ ./pf.out f(x) = x; f(x) = -x; f(x) = x ; 4. f(x) = - x; f(x) = (3* cos(x) -2*sin (4+x) -1)/2; inio Exit Selec

f(x) -X - * -- - - - - - - - - - - - -1.6000 -1.3246 -1.5000 -1.6762 -1.4000 -1.9662 -1.3000 -2.1174 -1.2000 -2.0752 -1.1000

icinsiwo (x) = x; f(x) = -x; f(x) = x ; f(x) = - x ; f(x) = (3* cos(x) -2*sin (4*x) -1)/2; Exit Selection: 0 [user123@ssh] $

I will upvote if u will solve
Thank you for your time

Write this code in c

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>
#include<math.h>
double func1(double x)
{
return x;
}
double func2(double x)
{
return x*(-1);
}
double func3(double x)
{
return x*x;
}
double func4(double x)
{
return x*x*x*(-1);
}
double func5(double x)
{
return (3*cos(x)-(2*sin(4*x))-1)/2;
}
int main()
{
double from,to,interval,min=0,max=0,minx,maxx,result,a;
int choice;
do
{
printf("1.f(x)=x\n2.f(x)=-x\n3.f(x)=x^2\n4.f(x)=-x^3\n5.f(x)=(3*cos(x)-2*(4*x)-1)/2\n0.Exit\n");
printf("Selection: ");
scanf("%d",&choice);
if(choice==0)
return 0;
printf("From: ");
scanf("%lf",&from);
printf("To: ");
scanf("%lf",&to);
printf("Step: ");
scanf("%lf",&interval);
switch(choice)
{
case 1:a=from;
while(a<=to)
{
result=func1(a);
if(result<=min)
{
min=result;
minx=a;
}
if(result>=max)
{max=result;
maxx=a;
}
a+=interval;
}
printf("Min y=%lf for x=%lf\n",min,minx);
printf("Max y=%lf for x=%lf",max,maxx);
a=from;
printf("\n\tx\tf(x)\n");
while(a<=to)
{ result=func1(a);
printf("%lf\t%lf\n",a,result);
a+=interval;
}
break;
case 2:a=from;
while(a<=to)
{
result=func2(a);
if(result<=min)
{
min=result;
minx=a;
}
if(result>=max)
{max=result;
maxx=a;
}
a+=interval;
}
printf("Min y=%lf for x=%lf\n",min,minx);
printf("Max y=%lf for x=%lf",max,maxx);
a=from;
printf("\nx\tf(x)\n");
while(a<=to)
{ result=func2(a);
printf("%lf\t%lf\n",result,a);
a+=interval;
}
break;
case 3:a=from;
while(a<=to)
{
result=func3(a);
if(result<=min)
{
min=result;
minx=a;
}
if(result>=max)
{max=result;
maxx=a;
}
a+=interval;
}
printf("Min y=%lf for x=%lf\n",min,minx);
printf("Max y=%lf for x=%lf",max,maxx);
a=from;
printf("\nx\tf(x)\n");
while(a<=to)
{ result=func3(a);
printf("%lf\t%lf\n",result,a);
a+=interval;
}
break;
case 4:a=from;
while(a<=to)
{
result=func4(a);
if(result<=min)
{
min=result;
minx=a;
}
if(result>=max)
{max=result;
maxx=a;
}
a+=interval;
}
printf("Min y=%lf for x=%lf\n",min,minx);
printf("Max y=%lf for x=%lf",max,maxx);
a=from;
printf("\nx\tf(x)\n");
while(a<=to)
{ result=func4(a);
printf("%lf\t%lf\n",result,a);
a+=interval;
}
break;
case 5:a=from;
while(a<=to)
{
result=func5(a);
if(result<=min)
{
min=result;
minx=a;
}
if(result>=max)
{max=result;
maxx=a;
}
a+=interval;
}
printf("Min y=%lf for x=%lf\n",min,minx);
printf("Max y=%lf for x=%lf",max,maxx);
a=from;
printf("\nx\tf(x)\n");
while(a<=to)
{ result=func5(a);
printf("%lf\t%lf\n",result,a);
a+=interval;
}
break;
default:printf("Invalid Input");
}
}while(1);
return 0;
}

This is the program for the above question but the sample output provided by you does not match with the expression for the fifth case i.e (3*cos(x)-2*sin(4*x)-1)/2.I even tried manually and the answers matched with the answer generated by the program but are different from those provided in the sample output.Some brackets may be missing in the expression,just change the expression in the func5 accordingly for the right answer.Other than that everything else is working fine.

Add a comment
Know the answer?
Add Answer to:
I will upvote if u will solve Thank you for your time Write this code in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please answer all of the above question. 1. Suppose that the step response of a 2nd...

    Please answer all of the above question. 1. Suppose that the step response of a 2nd order underdamped system has been measured and recorded in Table I, where the first column denotes the time in second and the second column denotes the measured output. Obtain the transfer function for this system. Graph the response using an Excel graph or chart. Table I: Step Response of a 2nd Order Underdamped System Time (seconds) Step Response 0 0.1000 0.2000 0.3000 0.4000 0.5000...

  •      I need solution as soon as possible thank you Q1 Given, f(x) = { 4,0$*<2...

         I need solution as soon as possible thank you Q1 Given, f(x) = { 4,0$*<2 4x +1,25x<4 (a) Sketch the graph of f(x) and its even half-range expansion. Then sketch THREE (3) full periods of the periodic function in the interval -12 < x < 12. (6 marks) (b) Determine the Fourier cosine coefficients of Q1(a). (10 marks) (C) Write out f(x) in terms of Fourier coefficients you have found in Q1(b). (4 marks) SOME RELEVANT FORMULA Fourier Series...

  • please code on MATHLAB and show graph thank you in advance! Generate the following function y1(t)=2*...

    please code on MATHLAB and show graph thank you in advance! Generate the following function y1(t)=2* sin (2*pi* 1 *t)+4*sin(2*pi*2*t)+3*sin(2*pi*4*t) y2(t)=2*cos(2*pi*1*t)+4*cos(2*pi*2*t)+3*cos(2*pi*4*t) First, required by sampling theorem, the step size of t (delta t) has to be smaller than a value. Figure out this value and set the delta t at least 3 times smaller than this value and calculated y1(t) and y2(t). Increase the step size (delta t) and recalculate y1(t) and y2(t). See the difference. Calculate Fourier Transform of...

  • You are given a finite step function xt=-1  0<t<4 1  4<t<8.           Hand calculate the FS coefficients of...

    You are given a finite step function xt=-1  0<t<4 1  4<t<8.           Hand calculate the FS coefficients of x(t) by assuming half- range expansion, for each case below. Modify the code below to approximate x(t) by cosine series only (This is even-half range expansion). Modify the below code and plot the approximation showing its steps changing by included number of FS terms in the approximation. Modify the code below to approximate x(t) by sine series only (This is odd-half range expansion).. Modify...

  • Use Matlab code Consider the following function sin(x) Using the following parameters in your functions: -func:...

    Use Matlab code Consider the following function sin(x) Using the following parameters in your functions: -func: the function/equation that you are required to integrate -a, b: the integration limits n: the number of points to be used for the integration I:Integral estimate a) Write a function capable of performing numerical integration of h(x) using the composite trapezoidal rule. Use your function to integration the equation with 9 points. Write a function capable of performing numerical integration of h(x) using the...

  • Explain step by step how to solve this problem. I need it ASAP. Problem 4. ngsYou...

    Explain step by step how to solve this problem. I need it ASAP. Problem 4. ngsYou Chapter 2pds tps://csīw.csicuryedu/supernova 7/Rles/robbins/CSC270/LECTUREREVENN/Chapter%202 pdf ADVANCED HAND IN HW #3 READ STUDY 2.4.2.5.2.7,28.2.9 (skip 2.3,2.6) DO THE FOLLOWING EXAM PRACTICE part 1. PROBLEMS AT THE END OF CHAPTER 2. #14 TO #20 Both editions of the text, 10 PTS part2 #2, write the c++ code for this formula 27 points T- 2T(Lcos()/g)12 3 PTS 3. Write the algebraic formula for the following c++ code...

  • Hello! I need some help with these questions. It would be awesome if you could write...

    Hello! I need some help with these questions. It would be awesome if you could write the answer choice (A,B,C,D...etc.) because sometimes it’s hard for me to read the final answer in certain handwriting styles. Thank you so much!! To verify the identity tan(++an tanattan x2+tanろーtanatanxatan-what should 1-tan x tan 2tan 2 tan x3-tan tan x3 you do first? tan x +tan 1+ tan A tan xo tan tan xi-tan (x2 + Otan(q+x2+13) = 1+tan xi tan (x2 +13) O...

  • Undecimal to decimal&decimal to undecimal #Your code here Thank you! Binary-to-Decimal In a previous lab, we...

    Undecimal to decimal&decimal to undecimal #Your code here Thank you! Binary-to-Decimal In a previous lab, we considered converting a byte string to decimal. What about converting a binary string of arbitrary length to decimal? Given a binary string of an arbitrarily length k, bk-1....bi .box the decimal number can be computed by the formula 20 .bo +21.b, + ... + 2k-1. bx-1- In mathematics, we use the summation notation to write the above formula: k- 2.b; i=0) In a program,...

  • This is due in a few hours I don't know if I did this correctly can you double check my code to see if my return statements are fine and I follow the instructions accurately. Thank you!!! Modify m...

    This is due in a few hours I don't know if I did this correctly can you double check my code to see if my return statements are fine and I follow the instructions accurately. Thank you!!! Modify my code if anything return statement seems misplaced or inaccurately called. Here is my code: int write_sudoku_board(const char file_name[ ], int board[9][9]) { int number; int i,j; int a = 9; FILE* fp = fopen("sudoku.txt", "w"); int count = 0; for(i =...

  • the code in the photo for this I.V.P dy/dx= x+y. y(0)=1 i need the two in the photo thank you New folder Bookmark...

    the code in the photo for this I.V.P dy/dx= x+y. y(0)=1 i need the two in the photo thank you New folder Bookmarks G Google dy/dx x+y, y(0)=1 2 h Exact Solution 1.8 Approximate Solution Mesh Points 1.6 -Direction Fied 1.4 1.2 1 0.8 04 0.2 0.3 0.1 0 X CAUsersleskandara\Desktop\New folder emo.m EDITOR PUBLISH VEW Run Section FILE NAVIGATE EDIT Breakpoints Run Run and FL Advance Run and Advance Time BREAKPOINTS RUN 1 - clear all 2 clc 3-...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT