Troubleshooting a MATLAB Error: Inner Matrix Dimensions Must Agree

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
5 replies · 3K views
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
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?
 
I like Serena said:
It looks right to me. (Nod)

Nice... (Whew) Thank you!