Solving MATLAB Plotting Error for x(t) Function

In summary, the issue with the period of the plotted function in MATLAB is likely caused by the incorrect computation of the angle of the Fourier coefficients in the given code. To fix this, you should use different values for the angle for each coefficient, which will result in a period of 5 for the function.
  • #1
farhadd
3
0
Hello,

I've written a piece of code in MATLAB, to depict the function x(t)

I have the Fourier coefficients of this function as following:

a(k) = 1/(k.*pi)^2

where k is even and otherwise a(k) = 0.

also we know that x(t+5) = x(t)

my code is :
Code:
function [zaribX] = Ak(k)  

    if ~((mod(k,2) == 1) || k==0)  
        zaribX=1./(k.*pi).^2; 
    else zaribX=0;
    end
return;
end

function [x] = FunctionX(Kmax,t)
x = 0;
	for m = 0:Kmax
		f = 2 .* abs(Ak(m)) .* cos( m .* ((2.*pi)./5) .* t + angle(Ak(m)));
		x = x+f;
	end
return;
end

problem is, when MATLAB plots the function, it appears to have a period of 2.5 , and not 5. Could anyone help me see what's going wrong? Thanks a lot.

kcjt05.png
 
Physics news on Phys.org
  • #2
The problem is likely coming from the angle of the Fourier coefficients. In your code, you are computing the angle of each coefficient as angle(Ak(m)). However, this will yield a value of zero for all m, resulting in a period of 2.5. You should instead use a different value for the angle for each Fourier coefficient - for example, 0 for m=0 and pi/2 for m>0. This should result in a period of 5 for the function.
 

1. Why am I getting an error when trying to plot my x(t) function in MATLAB?

There could be several reasons for this error. One potential cause is that there is a mistake in your code, such as a misspelled variable name or missing parentheses. Another possibility is that your function is not properly defined, leading to an error when trying to plot it. It is also important to check if your function is valid for the range of values you are trying to plot.

2. How can I fix the error "x(t) is undefined" when plotting my function in MATLAB?

This error means that your function x(t) has not been properly defined in your code. Make sure that you have correctly defined the input variables and the output function. You may also need to check the syntax of your function and make sure it follows MATLAB's conventions.

3. What does the error "Subscript indices must either be real positive integers or logicals" mean when plotting x(t) in MATLAB?

This error usually occurs when trying to access an element in a matrix or array using an index that is not a real positive integer or a logical value. This can happen when the index is a decimal or a negative number. Check your code and make sure that you are using valid indices when plotting your function.

4. How do I troubleshoot the error "Out of memory" when plotting my x(t) function in MATLAB?

This error means that your computer does not have enough memory to complete the plotting process. One way to fix this is to reduce the size of your data or to use the "plot" function instead of "plot3" if you are plotting in 3D. You can also try closing other programs or restarting your computer to free up memory.

5. Can using incorrect input values cause an error when plotting x(t) in MATLAB?

Yes, using incorrect input values can lead to errors when plotting your function in MATLAB. Make sure that your input values are within the valid range for your function and that they are of the correct data type. If your function is sensitive to certain input values, you may also need to handle potential errors or exceptions in your code.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
553
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
211
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
983
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
833
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
3K
Back
Top