# Error in defining symbolic function

3 visualizaciones (últimos 30 días)
Akshay Kumar el 28 de Jun. de 2021
Respondida: Amit Bhowmick el 29 de Jun. de 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
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Amit Bhowmick el 29 de Jun. de 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;
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Categorías

Más información sobre Operations on Strings en Help Center y File Exchange.

R2020b

### Community Treasure Hunt

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

Start Hunting!

Translated by