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 Comments

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?
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.

Sign in to comment.

 Accepted Answer

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

More Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Asked:

on 11 Jan 2019

Answered:

on 11 Jan 2019

Community Treasure Hunt

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

Start Hunting!