Modify the original codes that we developed for sin(x), cos(x) and exp(x) (you c
ID: 3813779 • Letter: M
Question
Modify the original codes that we developed for sin(x), cos(x) and exp(x) (you can also refer to the “Additional Information”) according the following design criteria:
(50 points) For exp(x), separate the integer from the fractional component of the argument. For example if the function is called as exp(3.123), separate the 3 from the fractional component 0.123. Tip: you can use x-floor(x) to do this. Recognizing that exp(x+y)=exp(x)*exp(y), compute the integer portion of the argument using a simple loop that performs the multiplication and calculate the fractional component using the series expansion.
Additional Information:
You may use the following code fragments for the sin(x), cos(x), and exp(x) functions. Note, however, that these routines calculate a fixed number of terms, based on the value of N. you will have to modify the routines to continue to calculate terms until the accuracy criterion has been established. Tips: do NOT try to write the complete, fully-functional routine all at once! Better is to work on the individual pieces, test them independently, then assemble together for the final functional version.
% calculate exp(x), given x and N (number of terms)
s= 1; % this is the sum of terms and our final answer
xt= 1; % first term is 1, so let's just put it in to start
fact= 1; % factorial of first term is also 1, so put it in here
for i=1:N % now lets add N terms, as requested by user
xt= xt*x; % calculate numerator of next term (x^i)
fact= fact*i; % calculate denominator of next term (i!)
s=s + xt/fact; % add term to total sum, and go back for more
end
% calculate sin(x), given x and N (number of terms)
s=x; % 1st term in series is x, so put it here
xt=x; % used to compute next term
fact=1; % factorial of first terms is 1 also, so put here
sgn=1; % here is the sign. We start with +1.
for i=3:2:N
sgn= -sgn; % cheap way to alternate sign on each successive term
xt= xt*x*x; % numerator: x^i, given x^(i-2) from previous time
fact= fact*i*(i-1); % calculate denominator: factorial, skipping by 2
s= s + sgn*xt/fact; % add term to total sum
end
% calculate cos(x), given x and N (number of terms)
s= 1;
xt= 1;
fact= 1;
sgn= 1;
for i=2:2:N
sgn= -sgn;
xt=xt*x*x;
fact=fact*i*(i-1);
s=s + sgn*xt/fact;
end
Explanation / Answer
import java.awt.*;
import java.awt.event.*;
/*********************************************/
public category MyCalculator extends Frame
mathematician setClear=true;
double variety, memValue;
char op;
String digitButtonText[] = ;
String operatorButtonText[] = ;
String memoryButtonText[] = ;
String specialButtonText[] = ;
MyDigitButton digitButton[]=new MyDigitButton[digitButtonText.length];
MyOperatorButton operatorButton[]=new MyOperatorButton[operatorButtonText.length];
MyMemoryButton memoryButton[]=new MyMemoryButton[memoryButtonText.length];
MySpecialButton specialButton[]=new MySpecialButton[specialButtonText.length];
Label displayLabel=new Label("0",Label.RIGHT);
Label memLabel=new Label(" ",Label.RIGHT);
final int FRAME_WIDTH=325,FRAME_HEIGHT=325;
final int HEIGHT=30, WIDTH=30, H_SPACE=10,V_SPACE=10;
final int TOPX=30, TOPY=50;
///////////////////////////
MyCalculator(String frameText)//constructor
//set Co-ordinates for Special Buttons
tempX=TOPX+1*(WIDTH+H_SPACE); y=TOPY+1*(HEIGHT+V_SPACE);
for(int i=0;i<specialButton.length;i++)
//set Co-ordinates for Digit Buttons
int digitX=TOPX+WIDTH+H_SPACE;
int digitY=TOPY+2*(HEIGHT+V_SPACE);
tempX=digitX; y=digitY;
for(int i=0;i<digitButton.length;i++)
}
//set Co-ordinates for Operator Buttons
int opsX=digitX+2*(WIDTH+H_SPACE)+H_SPACE;
int opsY=digitY;
tempX=opsX; y=opsY;
for(int i=0;i<operatorButton.length;i++)
}
addWindowListener(new WindowAdapter()
});
setLayout(null);
setSize(FRAME_WIDTH,FRAME_HEIGHT);
setVisible(true);
}
//////////////////////////////////
static String getFormattedText(double temp)
////////////////////////////////////////////////
static mathematician isInString(String s, char ch)
come true;
return false;
}
/////////////////////////////////////////////////
public void actionPerformed(ActionEvent ev)
catch(NumberFormatException e)
if (index==0 && cl.displayLabel.getText().equals("0")) return;
if(cl.setClear)
zero.");}
return;
}
if(opText.equals("sqrt"))
catch(ArithmeticException excp)
// method = button ironed
switch(cl.op)
catch(ArithmeticException excp)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.