MATLAB Matrix dimensional error in numerical integration

In summary, when trying to numerically integrate an equation using the trapezium method, MATLAB errors out because the matrix dimensions do not agree. To solve this problem, you need to use a "." before the operation to indicate that this is a component by component operation.
  • #1
sarrfriend
6
0
Hi,
I tried to numerically integrate an equation in MATLAB using the trapezium method. Here is the code that I wrote:

ao = 1;
X = 0:ao/10:ao;
Y = 1/sqrt((power(Ho,2)*((O_m*power(X,-1))+(O_r*power(X,-2))+(O_l*power(X,2))))-(k*power(c,2)));
age = trapz(X,Y);

where the values for O_m, O_r, O_l and k were obtained using input(). When I executed the above code, I get "Error using / Matrix dimensions must agree."

Can you tell me what the problem is?

Sarrvesh
 
Physics news on Phys.org
  • #2
sarrfriend said:
Hi,
I tried to numerically integrate an equation in MATLAB using the trapezium method. Here is the code that I wrote:

ao = 1;
X = 0:ao/10:ao;
Y = 1/sqrt((power(Ho,2)*((O_m*power(X,-1))+(O_r*power(X,-2))+(O_l*power(X,2))))-(k*power(c,2)));
age = trapz(X,Y);

where the values for O_m, O_r, O_l and k were obtained using input(). When I executed the above code, I get "Error using / Matrix dimensions must agree."

Can you tell me what the problem is?

Sarrvesh

Hey sarrfriend.

MATLABs main data structure is a matrix and it treats pretty much everything in that way. This means that if you do say a * b, a + b and so on, then it assumes that this is going to be a matrix operation and it also assumes that the operations do things in terms of matrix assumptions (AxB a is axb, B is bxc and so on). This is why you get the error.

What you need to do if you want to do a component by component arithmetic operation is put a "." before the operation. For example let's say we have a vector A [0,1,2,3,4,5] and another vector B [0,2,4,6,8,10]. Then if we write C = A .* B we get [0,2,8,18,32,50]. If you want to do exponentiation just use .^ operator instead of power which will do a component wise exponentiation.

If you want to know more create a few row vectors of the same size and try .* ./ and .^ and see what you get in the output.
 
  • #3
Hi Chiro,
Thanks the info. It worked.

Sarrfriend
 

Related to MATLAB Matrix dimensional error in numerical integration

What is a MATLAB matrix dimensional error in numerical integration?

A MATLAB matrix dimensional error in numerical integration occurs when the dimensions of the matrices used in the integration process do not match. This can happen when trying to perform operations on matrices with different sizes or when trying to integrate a function that is not compatible with the dimensions of the matrices.

How can I fix a MATLAB matrix dimensional error in numerical integration?

There are a few ways to fix a MATLAB matrix dimensional error in numerical integration. One option is to double check that the matrices being used have compatible dimensions and make any necessary adjustments. Another option is to use the "reshape" function to change the dimensions of the matrices to match. It is also important to ensure that the function being integrated is compatible with the dimensions of the matrices.

Why am I getting a MATLAB matrix dimensional error in numerical integration?

There are a few common reasons for getting a MATLAB matrix dimensional error in numerical integration. One possible reason is that the matrices being used have different sizes or dimensions. Another reason could be that the function being integrated is not compatible with the dimensions of the matrices. It is also possible that there is an error in the code or a mistake in the input data.

Can I prevent a MATLAB matrix dimensional error in numerical integration?

Yes, there are steps you can take to prevent a MATLAB matrix dimensional error in numerical integration. Double check that the matrices being used have compatible dimensions and make any necessary adjustments. Also, ensure that the function being integrated is compatible with the dimensions of the matrices. It can also be helpful to use the "reshape" function to adjust the dimensions of the matrices if needed.

Are there any resources available for troubleshooting MATLAB matrix dimensional errors in numerical integration?

Yes, there are many resources available for troubleshooting MATLAB matrix dimensional errors in numerical integration. The MATLAB documentation and online forums are great places to start. There are also tutorials and guides available that provide tips and solutions for common errors. Additionally, seeking help from a more experienced MATLAB user or consulting with a MATLAB expert can also be beneficial.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
27
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
Back
Top