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

조회 수: 22 (최근 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일
편집: 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에서 Conversion Between Symbolic and Numeric에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by