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

16 views (last 30 days)
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.

Accepted Answer

Sai Sumanth Korthiwada
Sai Sumanth Korthiwada on 31 Mar 2023
Edited: Sai Sumanth Korthiwada on 31 Mar 2023
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 Comment
Tim
Tim on 4 Apr 2023
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

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!