Help with matlab program for cos(x)

In summary, the conversation discusses creating a MATLAB function called seriesCos that approximates the function cos(x) using a series expansion. The function uses a while loop and continues adding terms to the series until the absolute value of the difference between successive iterations is less than 1.e-6. The conversation also includes a sample attempt at a solution, which has an error in the while loop condition that prevents it from running properly.
  • #1
baznguy
1
0

Homework Statement



The function cos(x) can be approximated using the following series expansion:
1df491c1c760cda57c0f4f71cd2f191.png


Write a MATLAB function called seriesCos that takes a single scalar argument, x, uses the above formula to compute cos(x), and returns the result. Your function should use a while loop and continue adding terms to the series until the absolute value of the difference between successive iterations is less than 1.e-6. MATLAB functions that may prove useful include factorial() and abs().

Homework Equations


i can't get it to work and I'm not sure what I am doing wrong.It's not going through the loop or anything

The Attempt at a Solution


function y=seriesCos(x);
%calculate cos(x) through a series
%user call seriesCos(x,n)
%x is number to evaluate n is desire cycles through series
x=input ('Enter x to be evalute: ');
ser=1;
sum=0;
n=1;
er=0.0;
while er>=1e-6
an=(-1)^n*x^(2*n)/(factorial(2*n));
sum=ser;
ser=sum+an;
n=n+1;
er=abs((ser-sum)/(ser));
end
disp (ser)
disp (er)
 
Physics news on Phys.org
  • #2
look at the line in which you start the while loop and you should see something wrong

what is er defined to be?
 
  • #3
You are right, it's not going through the loop, because you are telling it not to:

Code:
er=0.0;
while er>=1e-6 
 ...

I suggest setting er to something larger than 1e-6 to start with :P
 

1. How do I plot a cosine function in Matlab?

To plot a cosine function in Matlab, you can use the built-in cos() function. First, define the x values you want to plot, for example, x = 0:0.1:2*pi. Then, use the plot() function to plot the cosine values against the x values, like this: plot(x, cos(x)). Finally, use title(), xlabel(), and ylabel() to add a title and axis labels to your plot.

2. How do I change the color of my cosine plot in Matlab?

To change the color of your cosine plot in Matlab, you can use the plot() function's 'Color' argument. For example, to plot a red cosine function, you can use plot(x, cos(x), 'Color', 'r'). You can also use other color codes like 'b' for blue, 'g' for green, or 'k' for black.

3. How can I add a legend to my Matlab plot?

To add a legend to your Matlab plot, you can use the legend() function. This function takes in a cell array of strings as an argument, with each string representing a label for your plot. For example, if you have two plots, you can use legend({'Plot 1', 'Plot 2'}) to add a legend with the labels "Plot 1" and "Plot 2".

4. How do I calculate the value of cos(x) for a specific x value in Matlab?

To calculate the value of cos(x) for a specific x value in Matlab, you can simply use the cos() function with your desired x value as the argument. For example, if you want to find the cosine of 2, you can use cos(2) and Matlab will return the value of -0.4161.

5. How do I create a new variable that stores the values of cos(x) for a range of x values in Matlab?

To create a new variable that stores the values of cos(x) for a range of x values in Matlab, you can use the cos() function in combination with the : operator. For example, if you want to store the cosine values for x from 0 to 10, you can use cos_values = cos(0:10). This will create a new variable called cos_values that contains the cosine values for x from 0 to 10.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
811
  • Engineering and Comp Sci Homework Help
Replies
1
Views
955
  • Engineering and Comp Sci Homework Help
Replies
2
Views
826
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Back
Top