# Incorrect dimensions for matrix multiplication

8 views (last 30 days)
Mathew Aibinu on 31 May 2024
Edited: VBBV on 31 May 2024
clear all; close all;
% param value
k0 = 0.244; p=536.2; q=0.0000376;
% the function
k = @(t,u) (k0 + (p*k0.^u-q*k0)*t +(p*k0.^u-q*k0).*(p*u*k0.^u-q)*t.^2/factorial(2)...
+(p*k0.^u-q*k0).*((p*u*k0.^u-q).^2-p*u*(u-1)*k0.^(u-1)./2)*t.^3/factorial(3));
% grid
t = linspace(0,0.1);
u = linspace(0.3,0.9);
[T,U] = meshgrid(t,q);
% evaluate function
Z = k(T,U);
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To operate on each element of the matrix individually, use TIMES (.*) for elementwise multiplication.

Error in solution>@(t,u)(k0+(p*k0.^u-q*k0)*t+(p*k0.^u-q*k0).*(p*u*k0.^u-q)*t.^2/factorial(2)+(p*k0.^u-q*k0).*((p*u*k0.^u-q).^2-p*u*(u-1)*k0.^(u-1)./2)*t.^3/factorial(3)) (line 5)
k = @(t,u) (k0 + (p*k0.^u-q*k0)*t +(p*k0.^u-q*k0).*(p*u*k0.^u-q)*t.^2/factorial(2)...
% plot
figure
surf(T,U,Z)
%surf(T,Q,Z,'facecolor','none')
##### 0 CommentsShow -2 older commentsHide -2 older comments

Sign in to comment.

### Accepted Answer

VBBV on 31 May 2024
Edited: VBBV on 31 May 2024
```k = @(t,u) (k0 + (p*k0.^u-q*k0).*t +(p*k0.^u-q*k0).*(p*u*k0.^u-q).*t.^2/factorial(2)...
+(p*k0.^u-q*k0).*((p*u.*k0.^u-q).^2-p*u.*(u-1)*k0.^(u-1)./2).*t.^3/factorial(3));
% grid
```
##### 1 CommentShow -1 older commentsHide -1 older comments
VBBV on 31 May 2024
Edited: VBBV on 31 May 2024

Use element wise matrix multiplication in the anonymous function as above. Also use a finite number of elements for vectors t and u as below

```% grid
t = linspace(0,0.1,30);
u = linspace(0.3,0.9,30);
[T,U] = meshgrid(t,u);
```

Sign in to comment.

### Categories

Find more on Logical in Help Center and File Exchange

R2024a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!