当前位置:网站首页>The Generation of Matlab Symbolic Functions and the Calculation of Its Function Values
The Generation of Matlab Symbolic Functions and the Calculation of Its Function Values
2022-08-10 09:32:00 【Shaking the Mountain and the Moon】
I. Introduction
Symbolic function is a very important function of Matlab, which can be used to represent mathematical functions and also perform numerical calculations.There are many ways to create a symbolic function. This paper presents four methods to generate a symbolic function and to find the value of the symbolic function.
Four methods of generating symbolic functions: generating symbolic functions using string expressions, using syms to define symbolic variables to generate symbolic functions, using sym and @ to generate symbolic functions, and using function files to generate symbolic functions.
To find the function value of a symbolic function, you can use the command "matlabFunction" to convert the symbolic function into a representation that can calculate the function value mathematically to calculate the function value of the independent variable at some points.
Second, the generation of symbolic functions
1. The string method
is to write the function expression directly in the string, for example:
y = 'sin(x) - cos(x) + exp(x)'
You can get the running result:
y ='sin(x) - cos(x) + exp(x)'
At this point, the system will automatically identify x as an independent variable.
It should be noted that if some mathematical function symbols provided by the system are used, the argument must be enclosed in a pair of parentheses, otherwise the system will not recognize it (if you have to ask why, you can only answer "thisis the grammar rule of the system")
Note: This method is applicable to matlab7.1 and previous versions, and the new version simply treats it as a string.
2. The syms method (this method is the most commonly used method)
First use the syms command to define the independent variable, and then generate the symbolic function, for example:
syms x;y1 = sin(x) - cos(x) + exp(x)y2 = x^3 + 5*x^2 + 10*x + 1syms x yz = x * exp( -x^2 - y^2 )
Output result:
y1 =exp(x) - cos(x) + sin(x)y2 =x^3 + 5*x^2 + 10*x + 1z =x*exp(- x^2 - y^2)
When derivation of these functions, the system will automatically identify the independent variable. When encountering the derivative of a multivariate function, the default independent variable is the independent variable in the first position.For example
diff( y1 )diff( y2 )diff( z )% derivative function for the default argument xdiff( z, 'x' )% is equivalent to diff( z ), and can also be written as diff( z, x )diff( z, 'y' )% Derivative function for the specified independent variable y
The output is:
ans =3*x^2 + 10*x + 10ans =exp(- x^2 - y^2) - 2*x^2*exp(- x^2 - y^2)ans =exp(- x^2 - y^2) - 2*x^2*exp(- x^2 - y^2)ans =-2*x*y*exp(- x^2 - y^2)
3. Use sym and @ method
First use @ to declare variables, and write symbolic function expressions after them, then you can get symbolic functions.Then use sym to convert it into a symbolic function, and you can perform other symbolic operations, for example:
y = @(t)sin(t) - cos(t) + exp(t)ys = sym( y )dy = diff( ys )Idy = int( dy )fplot( y, [ -2*pi, 2*pi ] )figure;fplot( ys, [ -2*pi, 2*pi ] )figure;fplot( dy, [ -2*pi, 2*pi ] )
Output result:
y =function_handle with the following values:@(t)sin(t)-cos(t)+exp(t)ys =exp(t) - cos(t) + sin(t)dy =cos(t) + exp(t) + sin(t)Idy =exp(t) - 2^(1/2)*cos(t + pi/4)
4. The function file method
is to use the function file to generate the symbolic function.For example, the generating function x^n can be implemented as follows:
%xpower.mfunction output=xpower(n)syms xoutput=x^n;
3. Numerical operation of symbolic functions
For symbolic functions, in addition to symbolic operations, it is often necessary to calculate the function values of symbolic functions at certain points.The function value of the symbolic function can be calculated.For example:
clear allclcsyms x;y1 = sin(x) - cos(x) + exp(x);y2 = x^2 - 10*x + 16;y1f = matlabFunction( y1 );y2f = matlabFunction( y2 );x1 = [ 0, pi ];y1v = y1f( x1 )% Calculate the function value of the symbolic function y1f at x1, x1 can be a single coordinate, or an arrayx2 = [ 2 8 ];y2v = y2f( x2 )
Output result:
y1v =0 24.1407y2v =0 0
边栏推荐
猜你喜欢
【元宇宙欧米说】听兔迷兔如何从虚拟到现实创造潮玩新时代
07 【动态组件 组件注册】
ARM体系结构2:处理器内核和汇编指令集
浅析JWT安全问题
Fourier series and Fourier transform
[Metaverse Omi Says] See how UCOUCO integrates performance art into the Metaverse
凭借这份阿里架构师的万字面试手册,逆风翻盘,斩获阿里offer
J9数字论:关于DAO 特点的宏观分析
I don't want to do accounting anymore, Die changed to a new one, moved forward bravely, and finally successfully passed the career change test to double his monthly salary~
How to break the DeepFake face-changing scam?turn him over
随机推荐
打工人的第27天-平凡但不平淡的日子
CTFSHOW七夕杯web
[Data Architecture] Distributed Data Grid as a Solution for Centralized Data Monolith
13 【script setup 总结】
JWT:拥有我,即拥有权力
不想再干会计了,蝶变向新,勇往直前,最后成功通过转行测试实现月薪翻倍~
将博客搬至CSDN
Question brushing tool h
故障分析 | Sql_slave_skip_counter 使用不规范对复制的影响
mySQL增删改查进阶
2022 首期线下 Workshop!面向应用开发者们的数据应用体验日来了 | TiDB Workshop Day
Relearn bubble sort
【 WeChat applet 】 read page navigation
浅析JWT安全问题
ARM Architecture 3: Addressing and Exception Handling of ARM Instructions
2022年固定资产管理系统的概况
CAD to WPF: Tips on converting CAD drawing files to WPF vector code files (xaml files)
1499. 满足不等式的最大值 堆/双端队列
navicat导入SQL文件报:[ERR] 2006 - MySQL server has gone away [ERR] -- MySQL dump 10.13 Distrib 5.7.34
J9 Digital Theory: What kind of sparks will Web3.0+ Internet e-commerce cause?