Multiplying a vector times the inverse of a matrix in Matlab -


i have problem multiplying vector times inverse of matrix in matlab. code using following:

% final time t = 0.1;  % number of grid cells n=20; %n=40; l=20; % delta x dx=1/n  % define cell centers %x = 0+dx*0.5:dx:1-0.5*dx; x = linspace(-l/2, l/2, n)'; %define number of time steps ntime = 100; %nb! stability conditions-dersom ntime var 50 ville en fått helt feil svar pga lambda>0,5 %ntime = 30; %ntime = 10; %ntime = 20; %ntime = 4*21; %ntime = 4*19;   % time step dt dt = t/ntime  % define vector useful handling teh different cells j  = 1:n;    % number cells of domain j1 = 2:n-1;  % interior cells  j2 = 1:n-1;  % numbering of cell interfaces   %define vector initial data u0 = zeros(1,n); l = x<0.5; u0(l)  = 0; u0(~l) = 1;  plot(x,u0,'-r') grid on hold on  % define vector solution u = zeros(1,n); u_old = zeros(1,n);  % useful quantity discrete scheme r = dt/dx^2 mu     = dt/dx;  % calculate numerical solution u going through loop of ntime number % of time steps a=zeros(n,n);  alpha(1)=a(1,1); d(1)=alpha(1); b(1)=0;                 c(1)=b(1); gamma(1,2)=a(1,2);  % initial state u_old = u0;  pause  j = 2:ntime a(j,j)=1+2*r;     a(j,j-1)=-(1/dx^2);     a(j,j+1)=-(1/dx^2); u=u_old./a;     % plotting     plot(x,u,'-')     xlabel('x')     ylabel('p(x)')     hold on     grid on     % update "u_old" before move forward next time level     u_old = u;      pause   end  hold off 

the error message is:

matrix dimensions must agree.  error in implicit_new (line 72) u=u_old./a; 

my question therefore how possible perform u=u_old*[a^(-1)] in matlab?

david

as knedlsepp said, v./a elementwise division, not wanted. can use either

  • v/a provided v row vector , length equal number of columns in a. result row vector.

  • a\v provided v column vector , length equal number of rows in a

the results differ in shape: v/a transpose of a'\v'


Comments

Popular posts from this blog

java - Could not locate OpenAL library -

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

sorting - opencl Bitonic sort with 64 bits keys -