当前位置:网站首页>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 yThe 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边栏推荐
- 高等数学(第七版)同济大学 习题4-3 个人解答
- jq封装树形下拉选择框组件
- 浅谈DAO+DeFi+NFT模式开发代码技术方案丨链游元宇宙NFT盲盒项目技术开发逻辑(源码程序)
- 10 【异步组件 组合式函数(hooks)】
- 爬虫-爬取某小说网站
- 关于镜像源的一些记录
- js读取excel时间格式转换
- "Guangzhou highway engineering measures for the supervision and administration of production safety, and revised from six aspects
- 【元宇宙欧米说】听兔迷兔如何从虚拟到现实创造潮玩新时代
- PostgreSQL 2022 发展现状:13 个非 psql 工具
猜你喜欢
![[Data Architecture] Distributed Data Grid as a Solution for Centralized Data Monolith](/img/99/b22268e3e47db9e9caad58722e8709.png)
[Data Architecture] Distributed Data Grid as a Solution for Centralized Data Monolith

FPGA的虚拟时钟如何使用?

郭晶晶家的象棋私教,好家伙是个机器人

06 【生命周期 模板引用】

CAD to WPF: Tips on converting CAD drawing files to WPF vector code files (xaml files)

Vivado时序约束中Tcl命令的对象及属性

第三章 搜索与图论(三)

DAY26: GetShell project

CTFSHOW七夕杯web

怎么使用【jmeter正则表达式提取器】解决返回值作参数的问题
随机推荐
dayjs-----time format
Flink快速上手 完整使用 (第二章)
不想再干会计了,蝶变向新,勇往直前,最后成功通过转行测试实现月薪翻倍~
钻石价格预测的ML全流程!从模型构建调优道部署应用!
SQL优化总结
keepalived:主备配置
DAY26:GetShell专题
【元宇宙欧米说】听兔迷兔如何从虚拟到现实创造潮玩新时代
"Microservice Architecture" Arrangement and Choreography - Different Models for Making Systems Work Together
爬虫-爬取某小说网站
The first offline workshop in 2022!Data application experience day for application developers is coming | TiDB Workshop Day
Controller层代码这么写,简洁又优雅!
DAY25: Logic vulnerability recurrence
如何理解BIO、NIO、AIO的区别
数据库注入提权总结(一)
ARM Architecture 3: Addressing and Exception Handling of ARM Instructions
Fourier series and Fourier transform
【系统设计】S3 对象存储
PTA Exercise 2.2 Rotate an Array Left
【OAuth2】二十、OAuth2扩展协议 PKCE