matlab把符号数,Matlab 符号与数值之间的转换

本文介绍了在MATLAB中将符号数转换为数值的方法,包括使用digits函数设置精度,vpa函数进行变量精度算术计算,以及subs函数进行符号替换。通过示例展示了如何求解方程的精确解和不同精度的近似解,以及计算函数在特定点的数值值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

符号运算得到的是精确的解析解,但是有时需要进行数值转换,主要通过以下几个函数实现。

1.digits 函数

调用方法:digits(D)

函数设置有效数字个数为D的近似解精度。

2.vpa 函数

vpa

Variable-precision arithmetic

Syntax

R = vpa(A)

R = vpa(A,d)

Description

R = vpa(A) uses variable-precision arithmetic (VPA) to compute

each element of A to at least d decimal digits of accuracy, where d

is the current setting of digits.

R = vpa(A,d) uses at least d significant (nonzero) digits,

instead of the current setting of digits.

注:vpa中的精度是指有效值,即vpa(1.2345,2)=1.2;vpa(0.2345,2) = 0.23

3.subs函数

subs

Symbolic substitution

Syntax

g = subs(f,old,new)

g = subs(f,new)

g = subs(f)

g = subs(f,old,new,0)

Description

g = subs(f,old,new) replaces all occurrences of old with new in

f, and then evaluates f.

g = subs(f,new) replaces all occurrences of the default variable

in f (defined by symvar) with new , and then evaluates f.

g = subs(f) replaces symbolic variables in f with their values

obtained from the calling function and the MATLAB workspace, and

then evaluates f. Variables with no assigned values remain as

variables.

g = subs(f,old,new,0) prevents switching the arguments old and

new if subs(f,old,new) returns f. Without this additional argument,

the toolbox also tries subs(f,new,old) if subs(f,old,new) returns

f. This approach provides backwards compatibility with previous

versions and eliminates the need to remember the order of the

arguments.

例1:求方程3*x^2-exp(x) = 0 的精确解和各种精度的近似解

>> s = solve('3*x^2-exp(x) =

0')

s =

-2*lambertw(0, 3^(1/2)/6)

-2*lambertw(0, -3^(1/2)/6)

>> vpa(s)

ans =

-0.45896226753694851459857243243406

0.91000757248870906065733829575937

>> vpa(s,6)

ans =

-0.458962

0.910008

例2:设函数为f(x) = x – cos(x) 。求此函数在x = pi点的值的各种精度的数值近似形式。

>> x = sym('x');

>> f = x - cos(x)

f =

x - cos(x)

>> f1 = subs(f,x,pi)

f1 =

4.1416

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值