Error in defining symbolic function

3 次查看(过去 30 天)
Trying to define a symbolic function to calculate its derivative. My attempted code is attached.
I am getting following error " The following error occurred converting from sym to double:
Unable to convert expression containing symbolic variables into double array. Apply 'subs' function
first to substitute values for variables."
Any help will be highly appreciated.
close all; clear; clc;
syms g(a,b)
g = output(a,b);
function out = output(a,b)
for i = 1:10
p(i) = i^2+a*b;
if i==1
q(i) = 2;
else
q(i)= p(i-1);
end
end
out = sum(p.*q);
end

采纳的回答

Amit Bhowmick
Amit Bhowmick 2021-6-29
Hope this will work
close all;
clear;
clc;
syms a b g(a,b) %Changes
g = output(a,b);
function out = output(a,b)
for i = 1:10
p(i) = i^2+a*b;
if i==1
q(i) = str2sym(num2str(2));%Changes
else
q(i)= p(i-1);
end
end
out = sum(p.*q);
end
%output
%g =
%2*a*b + (a*b + 1)*(a*b + 4) + (a*b + 4)*(a*b + 9) + (a*b + 9)*(a*b + 16) + (a*b + 16)*(a*b + 25) + (a*b + 25)*(a*b + 36) + (a*b + 36)*(a*b + 49) + (a*b + 49)*(a*b + 64) + (a*b + 64)*(a*b + 81) + (a*b + 81)*(a*b + 100) + 2;

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

产品


版本

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by