简体   繁体   English

在matlab中评估函数

[英]Evaluating a function in matlab

I have expanded sin function into a Taylor series. 我已将sin函数扩展为泰勒级数。 Now I want to evaluate it at specific point. 现在我想在特定点评估它。 I get strange "MuPAD" errors in matlab. 我在matlab中遇到奇怪的“MuPAD”错误。 What am I doing wrong? 我究竟做错了什么?

function r1=taylor_sine
syms x;
mysine = taylor(sin(x), 63, 0);
r1 = funm(220, mysine); 

I'm not 100% familiar with the syntax you're using, maybe the inline function syntax is slightly different from the expanded syntax, however it looks like your function doesn't have a cleary defined input and output. 我不是100%熟悉你正在使用的语法,也许内联函数语法与扩展语法略有不同,但看起来你的函数没有一个明确定义的输入和输出。 A non-inline matlab function should look like this: 非内联matlab函数应如下所示:

%Comment
function [ theta ] = FunctionName( alpha, beta )
theta = alpha + beta;
end

Try to create your function in a separate .m file (filename the same as the function name). 尝试在单独的.m文件中创建函数(文件名与函数名称相同)。 After you've created the .m file make sure that it's located in the search path of MatLab (check if the autocompleter shows your function name when you type it in partially). 创建.m文件后,请确保它位于MatLab的搜索路径中(检查自动填充程序在部分键入时是否显示您的函数名称)。

As for the actual body of your function, I see a few weird things. 至于你的功能的实际身体,我看到一些奇怪的事情。 What is "syms x" supposed to do? 什么是“syms x”应该做什么? I would replace this line with "x = -pi:0.001:pi;" 我会用“x = -pi:0.001:pi;”替换这一行。 (have x be a vector from -pi to pi with increments of 0.001). (x是从-pi到pi的向量,增量为0.001)。 or something analogue to that. 或类似的东西。

Also for normal Taylor aproximation I would use taylor(sin(x), 63) (the overload with 'v'- does a Maclaurin approximation). 同样对于正常的泰勒近似,我会使用泰勒(sin(x),63)('v'的重载 - 进行Maclaurin近似)。 Also I wouldn't do a Taylor approximation up to the 63-1th order, that's way too high, maybe MatLab crashes on that. 此外,我不会做一个高达63-1顺序的泰勒近似,这太高了,也许MatLab崩溃了。

In the following picture you can see that the 7th order approximation is already extremely good between -pi and pi. 在下图中,您可以看到-pi和pi之间的7阶近似已经非常好。 对于sin(x)的不同逼近阶的泰勒级数

Did you really mean 220? 你真的是说220吗? Or did you mean 220 degrees in which case you should use 220*pi/180? 或者你的意思是220度,在这种情况下你应该使用220 * pi / 180?

I think it should be subs not funm 我觉得应该是潜艇funm

r1 = double( subs(mysine, x, 220*pi/180) );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM