Clear Filters
Clear Filters

Incorrect dimensions for matrix multiplication

6 views (last 30 days)
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')

Accepted Answer

VBBV
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 Comment
VBBV
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.

More Answers (0)

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!