Idhantsingh56 asked . 2020-07-31

How to build a symbolic function based on other symbolic functions?

As the codes shown below, I would like to build f4 based on f1 and f2, but an error occurred.

 

syms x y 
f1(x)=x+1;
f2(y)=y+2;
f3(x,y)=f1+y;% <-- nothing wrong
f4(x,y)=f1+f2% <-- where the error comes from
The error information:
Error using symfun/privResolveArgs (line 218)
Symbolic function input arguments must match.
 
Error in sym/privBinaryOp (line 1001)
args = privResolveArgs(A, B);
 
Error in + (line 7)
X = privBinaryOp(A, B, 'symobj::zipWithImplicitExpansion', '_plus');
 
Error in test_July_2020 (line 28)
f4(x,y)=f1+f2
 
Can someone please tell me why f3 can be built but f4? How to build a symfun based on other symfuns?

matlab , symbolic functions

Expert Answer

Prashant Kumar answered . 2024-05-19 05:35:07

When you use a symbolic function (by name, with no parameters) with a binary operator, and the other operand does not involve symbolic functions without parameters, then MATLAB can unambiguously construct a new symbolic function that takes the same arguments of the existing symbolic function and returns the appropriate value.
When you use a symbolic function (by name, with no parameters) with a binary operator, and the other operand involves a symbolic function with exactly the same number and order of parameters, then again MATLAB can unambiguously construct a new symbolic function that takes the same arguments of the existing symbolic function and returns the appropriate value.
However, when you use a symbolic function (by name, with no parameters) with a binary operator and the other operand involves a symbolic function with a different number or order of parameters, then MATLAB cannot know what the "right" order of parameters for the function to construct would be. When you add f1 (a function of just x) and f2 (a function of just y), then should the function that is created on the fly be a function of (x,y) or of (y,x) ?
It is important to understand for this purpose that this attempted automatic function construction is done first, without looking at the left hand side of the assignment.
The assignment statement
f3(x,y) = f1 + f2

is not treated like

function result = f3(x,y)
   result = f1 + f2
end

and is not treated like

f3 = @(x,y) f1 + f2

The f3(x,y) on the left hand side is not keyword abbreviation for constructing a true function.
Instead,

f3(x,y) = f1 + f2

is treated as

temporary = f1 + f2
f3 = symfun(temporary, [x, y])
to emphasize, the right hand side of the f3(x,y) = f1 + f2 is fully evaluated (probably returning a symbolic expression or symbolic function), and then turned into a symbolic function with the variable names given on the left hand side.
I do not mean this as an approximation of how MATLAB works internally: I mean that this is how MATLAB processes the line.
So, when f1 + f2 is trying to figure out whether to construct a temporary function of (x,y) or of (y,x), it does not have access to the context that eventually the output is going into a symfun that will have (x,y) as the inputs: the expression has to fully evaluate on its own, and that fails because it cannot reasonably decide the parameter order for the temporary function to be emitted.
Now if you had coded
 
f3(x,y) = f1(x) + f2(y)

then the result of f1(x) and of f2(y) would be expressions rather than symfun, and there is no problem adding expressions. Then, the expression fully constructed and inside an internal temporary variable,

f3 = symfun(temporary, [x,y])
would have no problem.
The error only occurs when you use symfun without parameters in expressions, and you encounter a situation in which the parameters do not match between two symfun.


Not satisfied with the answer ?? ASK NOW

Frequently Asked Questions

MATLAB offers tools for real-time AI applications, including Simulink for modeling and simulation. It can be used for developing algorithms and control systems for autonomous vehicles, robots, and other real-time AI systems.

MATLAB Online™ provides access to MATLAB® from your web browser. With MATLAB Online, your files are stored on MATLAB Drive™ and are available wherever you go. MATLAB Drive Connector synchronizes your files between your computers and MATLAB Online, providing offline access and eliminating the need to manually upload or download files. You can also run your files from the convenience of your smartphone or tablet by connecting to MathWorks® Cloud through the MATLAB Mobile™ app.

Yes, MATLAB provides tools and frameworks for deep learning, including the Deep Learning Toolbox. You can use MATLAB for tasks like building and training neural networks, image classification, and natural language processing.

MATLAB and Python are both popular choices for AI development. MATLAB is known for its ease of use in mathematical computations and its extensive toolbox for AI and machine learning. Python, on the other hand, has a vast ecosystem of libraries like TensorFlow and PyTorch. The choice depends on your preferences and project requirements.

You can find support, discussion forums, and a community of MATLAB users on the MATLAB website, Matlansolutions forums, and other AI-related online communities. Remember that MATLAB's capabilities in AI and machine learning continue to evolve, so staying updated with the latest features and resources is essential for effective AI development using MATLAB.

Without any hesitation the answer to this question is NO. The service we offer is 100% legal, legitimate and won't make you a cheater. Read and discover exactly what an essay writing service is and how when used correctly, is a valuable teaching aid and no more akin to cheating than a tutor's 'model essay' or the many published essay guides available from your local book shop. You should use the work as a reference and should not hand over the exact copy of it.

Matlabsolutions.com provides guaranteed satisfaction with a commitment to complete the work within time. Combined with our meticulous work ethics and extensive domain experience, We are the ideal partner for all your homework/assignment needs. We pledge to provide 24*7 support to dissolve all your academic doubts. We are composed of 300+ esteemed Matlab and other experts who have been empanelled after extensive research and quality check.

Matlabsolutions.com provides undivided attention to each Matlab assignment order with a methodical approach to solution. Our network span is not restricted to US, UK and Australia rather extends to countries like Singapore, Canada and UAE. Our Matlab assignment help services include Image Processing Assignments, Electrical Engineering Assignments, Matlab homework help, Matlab Research Paper help, Matlab Simulink help. Get your work done at the best price in industry.