Evaluate matrix equation at multiple timesteps

How can I evaluate a matrix equation at multiple timesteps? I've defined the following:
R=[1 0.5; 0.5 1];
M0=[1;1];
u0=[1;0];
x=linspace(0,1,100)
f=@(x) expm(-R.*x)*(u0-M0) + M0;
y=feval(f,x);
However, this gives an error as x is the 'wrong size' for matrix multiplication. However, I don't want to do matrix multiplication, but instead evaluate f at every value of x.

2 comentarios

Stephen23
Stephen23 el 11 de En. de 2019
Editada: Stephen23 el 11 de En. de 2019
Even if x is a scalar, your code produces multiple y values:
>> x = 1;
>> expm(-R.*x)*(u0-M0)
ans =
0.19170
-0.41483
Is this correct? Or do you expect one y value for each x value?
Mark
Mark el 11 de En. de 2019
I expect two y values for each x value. That's correct. I just want that to be repeated for each x value (effectively x is a series of timesteps and I want to evaluate this at time step). I'm not sure what the best way to do that is in matlab. I could write a for loop etc. but that seems unnecessarily complicated.

Iniciar sesión para comentar.

 Respuesta aceptada

Stephan
Stephan el 11 de En. de 2019

1 voto

Hi,
try:
R=[1 0.5; 0.5 1];
M0=[1;1];
u0=[1;0];
x=repmat(linspace(0,1,100),2,1);
y = zeros(2,size(x,2));
f=@(x) expm(-R.*x)*(u0-M0) + M0;
for k = 1:size(x,2)
y(:,k)=f(x(:,k));
end
plot(x(1,:),y(1,:),x(1,:),y(2,:))
result it:
result.PNG
Due to the matrix multiplication, i used a for loop. I think bsxfun should also be possible to do this.
Best regards
Stephan

Más respuestas (0)

Categorías

Más información sobre Mathematics en Centro de ayuda y File Exchange.

Preguntada:

el 11 de En. de 2019

Respondida:

el 11 de En. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by