失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > matlab 没有sym函数 错误使用== fprintf函数没有为'sym'输入定义

matlab 没有sym函数 错误使用== fprintf函数没有为'sym'输入定义

时间:2021-06-12 20:00:37

相关推荐

matlab 没有sym函数 错误使用== fprintf函数没有为'sym'输入定义

这是我的MATLAB代码。函数trapezoidal()是单独定义的,并且工作正常。错误使用==> fprintf函数没有为'sym'输入定义

syms x;

f = 10 + 2 * x - 6 * (x^2) + 5 * (x^4);

a = 0;

b = 2;

ans_3points = trapezoidal(f, a, b, 3);

ans_5points = trapezoidal(f, a, b, 5);

ans_7points = trapezoidal(f, a, b, 7);

fprintf('Integral estimate for three equally spaced points is %f.\n', ans_3points);

fprintf('Integral estimate for five equally spaced points is %f.\n', ans_5points);

fprintf('Integral estimate for seven equally spaced points is %f.\n', ans_7points);

actual_ans = int(f, 0, 2);

error_3points = 100 * (actual_ans - ans_3points)/actual_ans;

error_5points = 100 * (actual_ans - ans_5points)/actual_ans;

error_7points = 100 * (actual_ans - ans_7points)/actual_ans;

fprintf('Percentage relative error for three equally spaced points is %f.\n', error_3points);

fprintf('Percentage relative error for five equally spaced points is %f.\n', error_5points);

fprintf('Percentage relative error for seven equally spaced points is %f.\n', error_7points);

但是,这提供了以下错误的,打印error_3points行: ???错误使用==> fprintf 函数没有为'sym'输入定义。

我还没有在fprintf()中放入任何'sym'输入吗? ans_3points,ans_5points,ans_7points打印没有任何问题。 错误计算,但当我检查他们显示为分数。 这段代码究竟是什么问题?我真的无法弄清楚。 谢谢。

功能trapezoidal:

function l = trapezoidal(f, a, b, n)

N = n - 1; % N - the number of segmets

syms x;

series_sum = 0;

for i = (0 : (N - 1))

series_sum = series_sum + subs(f, x, xterm(i, a, b, n)) + subs(f, x, xterm((i + 1), a, b, n));

end

l = series_sum * (b - a)/(2 * N);

-09-11

Ruwangi

+0

'trapezoidal'返回什么数据类型? –

-09-11 09:27:20

+0

梯形函数返回一个浮点数。 –

-09-11 09:33:28

+0

你可以发布该功能吗? –

-09-11 09:38:43

如果觉得《matlab 没有sym函数 错误使用== fprintf函数没有为'sym'输入定义》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。