I'm no expert at all in mathematics but I wrote a simple program that doesn't seem to get the answer that I'd expect.
It provides:
ans =
6 - time
time + 6
as the only solutions, however shouldn't 0 be a solution assuming time > 6?
This could just be poor programming on behalf of me and if it is how would I get the required answer.
  1. Is this a bug?
  2. Am I making a mistake?
  3. If this is a mistake how can I fix it?
This is supposed to simulate a 1D particle traveling at a fixed velocity of 1 towards origin (0). I understand the answer is relatively simple to do by hand, however I have this repeated with more complicated variables in a larger equation that would be incredably tedious and unmaintainable to solve by hand.
syms position time;
speed = piecewise(position > 0,-1,position < 0,1,0);
solve(int(speed,time) + 6 == position,position) %6 is starting offset

 採用された回答

Walter Roberson
Walter Roberson 2019 年 8 月 28 日
編集済み: Walter Roberson 2019 年 8 月 29 日

1 投票

When position is 0 then speed is 0. int(0,time) is 0 plus a constant. Add 6 and the result is 6 plus a constant. The result is not equal to 0 unless the constant is -6. Which is possible, but int() always uses constant 0 for indefinite integration.

9 件のコメント

Dion Richardson
Dion Richardson 2019 年 8 月 29 日
編集済み: Dion Richardson 2019 年 8 月 29 日
Thanks Walter for the quick response.
Interesting, I thought using solve with + 6 would add 6 as the constact (+ c).
You do seem correct in that if I remove the constant it gives 0 as the only answer which would be correct.
Is it possible to add the constant still as int() doesn't seem to support it?
I've found an alternative that seems to get the correct result however but it doesn't really specify what happens when not in the bounds time [0,6)
syms position offset real;
syms time real positive;
speed = piecewise(position > 0,-1,position < 0,1,0);%6 is starting offset
ans = solve(int(speed,time) == position,position) %returns 0 correctly for +c of 0
clear
syms position offset real;
syms time real positive;
speed = piecewise(position > -6,-1,position < -6,1,0);%6 is starting offset
ans = solve(int(speed,time) == position,position) % returns -time when time < 6
Walter Roberson
Walter Roberson 2019 年 8 月 29 日
You can always
syms c
int(speed,time) + c
Dion Richardson
Dion Richardson 2019 年 8 月 29 日
However the solution doesn't seem to work when multiple piecewise are stacked.
syms position offset real;
syms time real positive;
speed = piecewise(position > -6,-1,position < -6,1,0);%6 is starting offset
speed2 = piecewise(position > -7,-1.5,position < -7,1.5,0);%6 is starting offset
[ans,param,cond] = solve(int(speed + speed2,time) == position,position, 'ReturnConditions', true)
Dion Richardson
Dion Richardson 2019 年 8 月 29 日
In my original answer I had int(speed,time) + 6 isn't that the same as int(speed,time) + c? Or is c a reserved name? I tried using int(speed,time) + offset and it had the same issues.
Walter Roberson
Walter Roberson 2019 年 8 月 29 日
c is not reserved.
You are hypthesizing that position 0 is a solution.
When position is 0, then speed = piecewise(position > 0,-1,position < 0,1,0) does not satisfy either of the first two conditions, so the result of the piecewise() is 0. So when position is 0, speed is 0. The indefinite integral of 0 is 0*time + arbitrary_constant -> arbitrary_constant . So you have established the equation solve(arbitrary_constant + 6 == 0, 0) and the first part of that can only be true when the arbitrary_constant is -6
syms arbitrary_constant
solve( int(speed, position) + arbitrary_constant + 6 == position, position)
If 6 is your arbitrary constant then 0 is not a solution
Dion Richardson
Dion Richardson 2019 年 8 月 29 日
syms position offset real;
syms time real positive;
speed = piecewise(position > 0,-1,position < 0,1,0);
[ans,param,cond] = solve(int(speed,time) + offset == position,position, 'ReturnConditions', true) %6 is starting offset
subs(ans,[time,offset],[60,5]) % returns 65 and -55
Ok so using that returns "wrong" values too. This might be due to me misunderstanding the conditions returned. However shouldn't 0 also be returned as a solution regardless?
Walter Roberson
Walter Roberson 2019 年 8 月 29 日
For reasons that are not obvious to me at the moment:
syms position offset real;
syms time real positive;
speed = piecewise(position > 0,-1,position < 0,1,0);
[ans,param,cond] = solve(simplify(int(speed,time) + offset == position),position, 'ReturnConditions', true) %6 is starting offset
subs(ans,[time,offset],[60,5])
Walter Roberson
Walter Roberson 2019 年 8 月 29 日
I filed a bug report about the discrepancy
Dion Richardson
Dion Richardson 2019 年 8 月 30 日
Sweet, thank you =)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

製品

リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by