MATLAB Troubleshooting a MATLAB Error: Inner Matrix Dimensions Must Agree

AI Thread Summary
The discussion focuses on troubleshooting a MATLAB error related to matrix dimensions in a code snippet for solving the heat equation using the backward Euler method. The main issue arises from the mismatch in lengths of the vectors x and t, leading to an "Inner matrix dimensions must agree" error. After adjusting the code to correctly index the approximation and exact solutions, the user successfully calculates the maximum error of the approximation for specific time steps. The results for the errors at n=100, 200, and 400 are presented, and they appear to be accurate. The conversation concludes with confirmation of the correctness of the results.
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
Views
1K
Replies
1
Views
2K
Replies
5
Views
2K
Replies
1
Views
3K
Replies
3
Views
15K
Back
Top