Error in defining symbolic function

4 Ansichten (letzte 30 Tage)
Akshay Kumar
Akshay Kumar am 28 Jun. 2021
Beantwortet: Amit Bhowmick am 29 Jun. 2021
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

Akzeptierte Antwort

Amit Bhowmick
Amit Bhowmick am 29 Jun. 2021
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;

Weitere Antworten (0)

Kategorien

Mehr zu Symbolic Math Toolbox finden Sie in Help Center und File Exchange

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by