Why I have this error: "Error using ==> minus Matrix dimensions must agree."

y0_d=[0; 0.2; -0.1];
dom_d=[0,3];
[xval_d,yval_d]=ode45('ex3_d_f',dom_d,y0_d)
And this is my funtion file:
function dy=ex3_d_f(x,y,z,t)
dy=zeros(3,1);
dy=[y-2.*z+sin(t)-2.*x; x+2.*z-cos(t)-2.*y; 3*x-3*y+5.*z];
Please correct my code

1 Comment

The function signature of ex3_d_f does not match that required by ode45.
The ode45 documentation explains that "The function dydt = odefun(t,y), for a scalar t and a column vector y, must return a column vector dydt...". Does this match your function definition? (hint: no, your function has four inputs).

Sign in to comment.

Answers (1)

You haven't given us all your inputs, so we can only guess.
It does appear your inputs to ex3_d_f are not in the correct order. Take a look at this example.
The error you are getting suggests at least one of the following is true
  • sin(t) and 2.*x are not the same size
  • 2.*z and cos(t) are not the same size
  • cos(t) and 2.*y are not the same size
  • or 3*x and 3*y are not the same size
When you subtract matrices, they must have the same size, or one must be a scalar.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 12 Apr 2021

Edited:

on 12 Apr 2021

Community Treasure Hunt

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

Start Hunting!