Effacer les filtres
Effacer les filtres

Incorrect dimensions for matrix multiplication

6 vues (au cours des 30 derniers jours)
Mathew Aibinu
Mathew Aibinu le 31 Mai 2024
Modifié(e) : VBBV le 31 Mai 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')

Réponse acceptée

VBBV
VBBV le 31 Mai 2024
Modifié(e) : VBBV le 31 Mai 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 commentaire
VBBV
VBBV le 31 Mai 2024
Modifié(e) : VBBV le 31 Mai 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);

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Shifting and Sorting Matrices dans Help Center et File Exchange

Produits


Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by