Conversion to double from function_handle is not possible.

29 views (last 30 days)
Cold someone help me with this. I'm trying to solve multiple ode s simultaneously. It says "Conversion to double from function_handle is not possible."
Here's the code
clc
clear all
[t,xa] = ode15s(@f,[0 80*3600],zeros(2499,1));
function dydt3 = f(t,x)
cpo=1e-7;
d=1.3;
p=5.7e-3;
dx=65/100;
dcdt=zeros(50,50);
for i=2:49
for j=2:49
dcdt(i,j)= @(x) (d*((x(i+1,j)+x(i-1,j)+x(i,j+1)+x(i,j-1)-4*x(i,j))/(dx^2)))
end
end
for j=2:49
dcdt(1,j)= @(x) (d*((x(1+1,j)+x(1+1,j)+x(1,j+1)+x(1,j-1)-4*x(1,j))/(dx^2)))
end
for i=2:49
dcdt(i,1)= @(x) (d*((x(i,1+1)+x(i,1+1)+x(i+1,1)+x(i-1,1)-4*x(i,i))/(dx^2)))
end
for j=2:49
dcdt(50,j)= @(x) (d*((x(50-1,j)+x(50-1,j)+x(50,j+1)+x(50,j-1)-4*x(50,j))/(dx^2)))
end
for i=2:49
dcdt(i,50)= @(x) (d*((x(i,50-1)+x(i,50-1)+x(i+1,50)+x(i-1,50)-4*x(i,50))/(dx^2)))
end
dcdt(50,1)=@(x) (d*((x(49,1)+x(49,1)+x(50,2)+x(50,2)- 4*x(50,1))/(dx^2)));
dcdt(50,50)=@(x) (d*((x(49,50)+x(49,50)+x(50,49)+x(50,49)-4*x(50,50))/(dx^2)));
dcdt(1,50)=@(x) (d*((x(1,49)+x(1,49)+x(2,50)+x(2,50)-4*x(1,50))/(dx^2)));
x(1,1)=@(x,t) ((d*x(1,2)+(dx*p*(cpo*((ap*exp(la.*t))+(1-ap)*exp(lb.*t)))))/(p*dx+d));
dcdt2= @(x,t) reshape(dcdt,[2500,1])
for i=1:2499
dcdt3(i)= dcdt2(i+1)
end
%dydt3 = dydt3';
end

Answers (1)

James Tursa
James Tursa on 4 Oct 2017
Edited: James Tursa on 4 Oct 2017
You can't store function handles inside a double variable. You must use a cell array instead. E.g.,
>> f = zeros(2,1);
>> f(1) = @(x)x^2
??? The following error occurred converting from function_handle to double:
Error using ==> double
Conversion to double from function_handle is not possible.
>> f = cell(2,1);
>> f{1} = @(x)x^2
f =
@(x)x^2
[]
>> f{2} = @(x)x^3
f =
@(x)x^2
@(x)x^3
>> f{1}(4)
ans =
16
>> f{2}(4)
ans =
64
That being said, it is not clear to me why you are trying to create an array of function handles.
  2 Comments
Lakshay Sethi
Lakshay Sethi on 4 Oct 2017
Thanks again but I'm new to matlab and not very familiar with the jargon. Could you please elaborate your answer or tell me what's wrong with my code.
Walter Roberson
Walter Roberson on 4 Oct 2017
Remove all of the @(x) and @(x,t). For example, rather than
dcdt(i,j)= @(x) (d*((x(i+1,j)+x(i-1,j)+x(i,j+1)+x(i,j-1)-4*x(i,j))/(dx^2)))
use
dcdt(i,j)= (d*((x(i+1,j)+x(i-1,j)+x(i,j+1)+x(i,j-1)-4*x(i,j))/(dx^2)));

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!