Expanding a function in Matlab

12 ビュー (過去 30 日間)
Teoman Selcuk
Teoman Selcuk 2021 年 11 月 14 日
コメント済み: John D'Errico 2021 年 11 月 14 日
I ma trying to writea code that expands the function (a + b).^2 into the expected output. Instead it gives the Output below. How would i be able to get to the Expected Output?
>> a = 'a';
>> b= 'b';
>> func = multip = (a + b).^2
Expected Output:
a^2+2ab+b^2
Output:
7414875

採用された回答

DGM
DGM 2021 年 11 月 14 日
Using symbolic tools:
syms a b
f = expand((a+b)^2)
f = 
Your approach would ostensibly return a number because you're treating characters as numbers.
double('a')
ans = 97
double('b')
ans = 98
(97+98)^2
ans = 38025
As to why exactly that number is different than what you got, I have no idea. The code you posted shouldn't do anything but throw an error.
  1 件のコメント
John D'Errico
John D'Errico 2021 年 11 月 14 日
I assume you are saying that what would throw an error is the line with func. Of course that is invalid MATLAB syntax.
a = 'a';
b = 'b';
func = multip = (a + b).^2
I note that
sqrt(7414875)
ans =
2723.02680853494
is not even an exact integer, but it is also far too large to be the sum of two standard ascii equivalents. So I have no immediate idea what was actually stored in those variables.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by