Error in defining symbolic function

3 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Amit Bhowmick
Amit Bhowmick il 29 Giu 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;

Più risposte (0)

Categorie

Scopri di più su Symbolic Math Toolbox in Help Center e File Exchange

Prodotti


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by