How to Resolve Vector Size Mismatch Error in MATLAB Convolution Fitting?

In summary: Your Name]In summary, the conversation discusses an issue with vector size mismatch while using a fitting routine to fit a function to a data set. The expert suggests adjusting the size of the input vectors or modifying the function to accept variable sizes in order to resolve the issue.
  • #1
Niles
1,866
0
Hi

I have the following piece of code:

Code:
dataX = -4:1:4;
dataY = [0 -1 -10 -40 -55 10 40 10 1];

figure(1)
plot(dataX, dataY, '*');

x = -2:0.01:2;
plot(-4:0.01:4, 1*conv(exp(-x).*heaviside(x),sin(x)), dataX, dataY, '*')

mdl = @(a, x)(a(1)*conv(exp(-x).*heaviside(x),sin(x)));

par=[1];
[fitted_par, r, J, cov, mse] = nlinfit(dataX, dataY, mdl, par);

What I am trying to do is to fit the function (a convolution) to the data set. However, I get an error due to vector size mismatch. I'm not sure what is going on here: The fitting routine should not care about how many data points I have?

Regards, Niels.
 
Physics news on Phys.org
  • #2


Hello Niels,

Thank you for sharing your code. It seems like the issue you are encountering is due to the size of your dataX and dataY vectors not matching the size of the x vector in your mdl function. The fitting routine does not care about the number of data points, but it does require that the input vectors have the same size.

In order to fix this, you can either adjust the size of your dataX and dataY vectors to match the size of x, or you can modify your mdl function to accept variable sizes for the input vectors. For example, you can use the "min" function to ensure that the input vectors are the same size before performing the convolution.

I hope this helps. Let me know if you have any further questions. Happy coding!

 

1. What is convolution in MATLAB?

In MATLAB, convolution is a mathematical operation that combines two signals, typically represented as vectors or matrices, and produces a third signal that represents the integration of the two original signals. It is often used in signal processing and image filtering tasks.

2. How do I fit a convolution in MATLAB?

To fit a convolution in MATLAB, you can use the conv function. This function takes two input signals as arguments and returns their convolution as an output signal. You can also use the conv2 function for two-dimensional convolution.

3. Can I specify the size of the output signal in MATLAB convolution?

Yes, you can specify the size of the output signal in MATLAB convolution by using the 'same' or 'full' options in the conv function. The 'same' option returns an output signal with the same size as the largest input signal, while the 'full' option returns the full convolution output.

4. What is the difference between linear and circular convolution in MATLAB?

The main difference between linear and circular convolution in MATLAB is how they handle boundary effects. Linear convolution extends the signals with zeros at the boundaries, while circular convolution wraps the signals around at the boundaries. This can lead to different results in the output signal.

5. How can I visualize the convolution process in MATLAB?

You can use the conv function in MATLAB to compute the convolution and then use the plot function to visualize the resulting output signal. Additionally, you can use the conv2 function for two-dimensional convolution and the imshow function to display the resulting image.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
Replies
1
Views
1K
Back
Top