Matlab code looking good? EE_Signals

In summary, the conversation is about a student asking for help with their MATLAB code for graphing two functions, x(t) = 9e^-2t and y(t) = 4e^(-9*pi*t), using convolution. They are unsure if their code is correct and are seeking guidance on how to properly set the time instances for plotting.
  • #1
user101
207
0

Homework Statement



Graph of x(t) = 9e^-2t for t>=0 and y(t) = 4e^(-9*pi*t).

Is my MATLAB code correct? I did it by hand and I'm getting a different answer, so I don't know which one is the correct one... if my hand one is correctly done or if the MATLAB one is correct. I'm learning MATLAB on my own since our professor doesn't require it... he thinks that we'll rely on it too much, but here's my attempt @ it.

Homework Equations


The Attempt at a Solution



t = 1:7;
x = 9*exp(-2*t);
v = 4*exp(-9*pi*t);
a = conv(x,v);
plot(t,a(1:length(t)))
xlabel('values for t')
ylabel('x(t) * v(t) value')
 
Physics news on Phys.org
  • #2
anyone?
 
  • #3
Why are you using convolution when you are only asked to graph the functions?
 
  • #4
he prob means graph the conv of the two functions.
 
  • #5
t = 1:7;

Careful with your vector, you need to set the time instances. 0.01 is usually a good one

t = 1:0.01:7;
 
  • #6
Your t value should be dependent on the function's piecewise linear limits. Did you pick a random t value?
 

1. How can I make my Matlab code look more organized and readable?

There are several ways to make your Matlab code look more organized and readable. First, use proper indentation and spacing to clearly show the structure of your code. Second, use meaningful variable names and comments to explain the purpose of each line of code. Third, break up long blocks of code into smaller functions or scripts. Finally, use consistent formatting and style throughout your code.

2. What are some best practices for writing efficient Matlab code?

To write efficient Matlab code, it's important to vectorize your operations whenever possible, as this can significantly improve performance. Additionally, avoid unnecessary loops and use built-in functions instead. It's also helpful to preallocate arrays and avoid excessive use of global variables.

3. How can I make my Matlab code more robust?

To make your Matlab code more robust, it's important to handle errors and exceptions appropriately. Use try-catch blocks to handle potential errors and provide informative error messages. You can also use input validation to prevent unexpected inputs from causing errors. Additionally, it's helpful to include unit tests in your code to catch any potential bugs.

4. Are there any tools or resources available to help improve the appearance of my Matlab code?

Yes, there are several tools and resources available to help improve the appearance of your Matlab code. The built-in Matlab Editor has features such as code folding and syntax highlighting that can make your code more visually appealing. There are also third-party tools, such as Matlab Code Analyzer, that can help identify potential issues and suggest improvements for your code.

5. How can I ensure my Matlab code follows good coding standards?

To ensure your Matlab code follows good coding standards, you can refer to the official Matlab Style Guidelines, which provide recommendations for formatting, naming conventions, and other coding practices. You can also use third-party tools that perform static code analysis and can identify any deviations from coding standards. Finally, regularly reviewing and refactoring your code can help maintain good coding practices.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
487
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
4K
Back
Top