Is it possible to display a multiplication operator in symbolic math's displayFormula output?

16 次查看(过去 30 天)
I am using the Symbolic Math Toolbox's function displayFormula() in Live Editor to display a series of equations and subsequent answers. When I try and show intermediate steps, filling the equations with the actual values found, where there would be a multiplication symbol (), it just outputs blank space. This works for variables where a*b displays , but with numbers 1*1 it displays as , which looks like eleven.
The code I'm working with:
syms x1 x2 x3 N1 N2 N3 s t N_i
dNs = "diff(N_i(s,t),s)"; % dN as a string for display
N1(s,t) = 1-s-t; % Shape functions
N2(s,t) = s;
N3(s,t) = t;
dN1s = diff(N1,s); % Calc partial derivatives
dN2s = diff(N2,s);
dN3s = diff(N3,s);
x1 = 0; % set x coords
x2 = 1;
x3 = 0;
sumdNsx = dN1s*x1+dN2s*x2+dN3s*x3;
displayFormula("symsum(dNs*x_i,i,1,3)==dN1s*x1+dN2s*x2+dN3s*x3==sumdNsx")
Output:
Should be something like:
or
* Side note: Wouldn't mind getting rid of the parentheses too.

采纳的回答

Sai Sumanth Korthiwada
编辑:Sai Sumanth Korthiwada 2023-3-31
Hi Tim,
As of Symbolic Math Toolbox R2022b, multiplication symbol (*) outputs a blank space for integers as well. Because the input given to the function gets evaluated. As a workaround, you can use
"'*'" % use this to add a '*' in between the formula
Also, pass a cell array of character vectors [ ] rather than passing a string, if you want to use '*' symbols in the formula.
syms x1 x2 x3 N1 N2 N3 s t N_i
dNs = "diff(N_i(s,t),s)"; % dN as a string for display
N1(s,t) = 1-s-t; % Shape functions
N2(s,t) = s;
N3(s,t) = t;
dN1s = diff(N1,s); % Calc partial derivatives
dN2s = diff(N2,s);
dN3s = diff(N3,s);
x1 = 0; % set x coords
x2 = 1;
x3 = 0;
sumdNsx = dN1s*x1+dN2s*x2+dN3s*x3;
% displayFormula("symsum(dNs*x_i,i,1,3)==dN1s*x1+dN2s*x2+dN3s*x3==sumdNsx")
%% instead try:
% to use '*' in the formula, workaround:
displayFormula(["symsum(dNs*x_i,i,1,3)==dN1s","'* '","x1+dN2s","'* '","x2+dN3s","'* '","x3==sumdNsx"])
% to use '(' in the formula, workaround:
displayFormula(["symsum(dNs*x_i,i,1,3)==dN1s","'('","x1","')+'","dN2s","'('","x2","')+'","dN3s","'('","x3","')='","sumdNsx"])
Hope this helps!
  1 个评论
Tim
Tim 2023-4-4
That works, thank you!
I do think when displaying integers symbolically, there should be a caveat in the program to display the operator, otherwise it displays incorrectly. But this work around does the trick for now. I will likely have to build a simple function to replace the * operators with a symbol so it can be applied repeatedly.
Appreciate you taking the time to answer my question. Cheers

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by