Troubleshooting a MATLAB Error: Inner Matrix Dimensions Must Agree

Click For Summary

Discussion Overview

The discussion revolves around troubleshooting a MATLAB error related to matrix dimension compatibility in a code snippet for a numerical method. Participants explore the causes of the error and share insights on calculating the maximum error in a finite difference method for approximating solutions to the heat equation.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant identifies that the error arises because the vectors x and t have different lengths, suggesting a potential adjustment to the definition of x.
  • Another participant discusses their implementation of the backward Euler finite difference method and the need to calculate the maximum error of the approximation.
  • A participant notes that the command used to access U_approx is incorrect, as it should reference the position of the vector t rather than its value.
  • After modifying the code, a participant shares their results for the errors of approximation at specific time steps and questions the validity of these results.
  • Several participants express agreement that the results appear correct, but no formal consensus is reached on the overall accuracy of the results.

Areas of Agreement / Disagreement

Participants generally agree on the identification of the initial error and the adjustments made to the code. However, there is no explicit consensus on the correctness of the final results presented.

Contextual Notes

The discussion includes assumptions about the definitions of variables and the structure of the MATLAB code, which may not be fully resolved. The exact nature of the error in the original code and the implications of the results remain open to interpretation.

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
4K
  • · Replies 11 ·
Replies
11
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
15K