Incorrect dimensions for matrix multiplication

7 次查看(过去 30 天)
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')

采纳的回答

VBBV
VBBV 2024-5-31
编辑:VBBV 2024-5-31
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 个评论
VBBV
VBBV 2024-5-31
编辑:VBBV 2024-5-31

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);

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by