Troubleshooting a MATLAB Error: Inner Matrix Dimensions Must Agree

Click For Summary
SUMMARY

The forum discussion addresses a MATLAB error related to matrix dimension mismatches in a finite difference method implementation for solving the heat equation. The user initially encounters an "Inner matrix dimensions must agree" error due to incompatible vector lengths for the variables x and t. The solution involves correcting the indexing in the code to ensure that the appropriate values are accessed, leading to successful error calculations for n=100, 200, and 400, with results indicating minimal approximation errors.

PREREQUISITES
  • Understanding of MATLAB programming and syntax
  • Familiarity with finite difference methods for numerical analysis
  • Knowledge of error analysis in numerical solutions
  • Basic concepts of matrix operations in MATLAB
NEXT STEPS
  • Explore MATLAB's matrix indexing techniques for multidimensional arrays
  • Learn about the implementation of finite difference methods in MATLAB
  • Research error analysis techniques for numerical approximations
  • Investigate MATLAB's built-in functions for norm calculations
USEFUL FOR

This discussion is beneficial for MATLAB programmers, numerical analysts, and anyone involved in computational mathematics, particularly those working with finite difference methods and error analysis in numerical solutions.

evinda
Gold Member
MHB
Messages
3,741
Reaction score
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
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 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:
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?
 
It looks right to me. (Nod)
 
I like Serena said:
It looks right to me. (Nod)

Nice... (Whew) Thank you!
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 11 ·
Replies
11
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
15K