Troubleshooting a MATLAB Error: Inner Matrix Dimensions Must Agree

In summary, the conversation involves discussing code written in Matlab for calculating the maximum error of an approximation using the backward euler finite difference method. The code is used to approximate the solution of the heat equation, and the conversation includes a question about finding the error in the code and a confirmation that the results appear to be correct.
  • #1
evinda
Gold Member
MHB
3,836
0
Hello! (Wave)

I have written the following code in matlab:
Code:
    function v=uexact(x,t)
      v=sin(2*pi*x)*exp(-4*pi^2*t);
    end
    
    function [ex]=test3
    
      h = 1/50; 
      T=1/2500; 
      
      x=0:h:1; 
      t=0:T:1; 
      
    
      
      ex=uexact(x,t);
    
    end
I get the following warning message:

[m]
? Error using ==> mtimes
Inner matrix dimensions must agree.

Error in ==> uexact at 3
v=sin(2*pi*x)*exp(-4*pi^2*t);

Error in ==> test3 at 11
ex=uexact(x,t);
[/m]Could you tell me where my mistake is? (Thinking)
 
Physics news on Phys.org
  • #2
Hey evinda! (Smile)

The vectors x and t have different lengths.
Perhaps it should be for instance [m]x=1:h:50[/m]? (Wondering)
 
  • #3
I like Serena said:
Hey evinda! (Smile)

The vectors x and t have different lengths.
Perhaps it should be for instance [m]x=1:h:50[/m]? (Wondering)

I will check it...
I have written a program in MATLAB for the backward euler finite difference method to approximate the solution of the heat equation.

I have to calculate the maximum error of the approximation using the backward euler method with $N_x=20$ subintervals of $[0,1]$ and $N_t=400$ time steps, at the time $t_n=n \tau$ (where $\tau =\frac{T_f}{N_t}$). So the formula that we have to use is $$E^n_{\text{IE}}:=\max_{1 \leq i\leq N_x+1} |u^n_i-u(t_n, x_i)|$$ for $n=100, 200, 400$.

($u(t_n, x_i)$ is the exact solution of the problem and $u^n_i$ is its approximation)

To find the error I have written the following code:

Code:
    function error_fin_dif_back_euler(a,b,Nx,Tf,Nt) 
    
    [u, ex]=finite_difference_backward_euler(a, b, Nx, Tf, Nt); 
    
    j=1; 
    i=100; 
     while (i<=400)
           n(j)=i; 
           U_approx=u(:,n(j)*Tf/Nt); 
           u_exact=ex(:,n(j)*Tf/Nt); 
           error=norm(U_approx-u_exact, inf); 
           disp(sprintf(' E(%f)= ',n(j), sfalma));
           j=j+1;
           i=2*i;
      end 
     
    end

The command [m]U_approx=u(:,n(j)*Tf/Nt);[/m] is wrong because the second coordinate must be the position of the vector $t$ at which the value is equal to [m]n(j)*Tf/Nt[/m], and not the value, since it is not an integer.

How can I do this? (Thinking)
 
Last edited:
  • #4
I changed the code as follows:

Code:
    function error_fin_dif_back_euler(a,b,Nx,Tf,Nt) 
    
    [u, ex]=finite_difference_backward_euler(a, b, Nx, Tf, Nt); 
    
    j=1; 
    i=100; 
     while (i<=400)
           n(j)=i; 
           U_approx=u(:,n(j)); 
           u_exact=ex(:,n(j)); 
           error=norm(U_approx-u_exact, inf); 
           disp(sprintf(' E(%d)= %.12f',n(j), error));
           j=j+1;
           i=2*i;
      end 
     
    end

I got the following errors of approximation for [m] n=100,200,400[/m]:
[m]
E(100)= 0.000039470949
E(200)= 0.000000005540
E(400)= 0.000000000000
[/m]Are the results right?
 
  • #5
It looks right to me. (Nod)
 
  • #6
I like Serena said:
It looks right to me. (Nod)

Nice... (Whew) Thank you!
 

1. What does the error "Inner Matrix Dimensions Must Agree" mean?

The error "Inner Matrix Dimensions Must Agree" means that there is a mismatch in the dimensions of matrices being used in a MATLAB operation. This can occur when trying to perform operations such as matrix multiplication, element-wise addition or subtraction, or concatenation.

2. Why am I getting this error?

You are getting this error because there is a mismatch in the dimensions of matrices you are trying to use in a MATLAB operation. This can happen when trying to combine matrices of different sizes or when performing operations that require matrices to have the same dimensions.

3. How do I fix this error?

To fix this error, you need to ensure that the matrices being used in the operation have compatible dimensions. This can be done by either resizing the matrices to have the same dimensions or by using functions such as reshape or repmat to create matrices with compatible dimensions.

4. Can I avoid this error?

Yes, this error can be avoided by carefully checking the dimensions of the matrices being used in an operation before performing it. It is also helpful to use MATLAB's built-in functions for matrix operations, as they often have built-in error checking and handling.

5. Are there any other common errors related to matrix dimensions in MATLAB?

Yes, other common errors related to matrix dimensions in MATLAB include "Matrix dimensions must agree" and "Dimensions of matrices being concatenated are not consistent". These errors indicate similar issues with mismatched dimensions in matrix operations.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
126
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
Back
Top