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

Stephen23
Stephen23 2019년 1월 11일
편집: Stephen23 2019년 1월 11일
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 2019년 1월 11일
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.

댓글을 달려면 로그인하십시오.

 채택된 답변

Stephan
Stephan 2019년 1월 11일

1 개 추천

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Mathematics에 대해 자세히 알아보기

질문:

2019년 1월 11일

답변:

2019년 1월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by