value of type 'sym' is not convertible to 'double' (2024)

188 次查看(过去 30 天)

显示 更早的评论

Sabrina Haumann 2022-8-9

  • 链接

    此问题的直接链接

    https://ww2.mathworks.cn/matlabcentral/answers/1776615-value-of-type-sym-is-not-convertible-to-double

  • 链接

    此问题的直接链接

    https://ww2.mathworks.cn/matlabcentral/answers/1776615-value-of-type-sym-is-not-convertible-to-double

评论: Sabrina Haumann 2022-8-9

Hey,

I am trying to let the following code run but I always get the same error:

Unable to perform assignment because value of type 'sym' is not convertible to 'double'.

Error in spdiags>makeSparseGeneral (line 225)

V(offset) = B(i + d(k), k);

Error in spdiags (line 125)

res1 = makeSparseGeneral(B, d, A);

Error in Combined_Model>spdiag (line 157)

x = spdiags(v,0,rv,rv);

Error in Combined_Model (line 76)

Js = spdiag(f(kc, kp) - (delta+n)*kc)*d1Mx*theta - ...

Caused by: Error using symengine. Unable to convert expression containing symbolic variables into double array. Apply 'subs' function first to substitute values for variables.

Can anyone help me with this?

%% COMBINED MODEL

clear; close all; clc;

%% Parameters

rho = 0.04;

theta = 1.5;

nu = 0.35;

delta = 0.05;

A=1;

L=1;

alpha = 0.5;

gamma = 0.5;

n = 0.01;

%% Functions

syms kc

Ec = (A*kc.^gamma*L.^(1-gamma)) ;

syms kp

Ep = (A*kp.^gamma*L.^(1-gamma)) ;

E = Ec + Ep;

fs = A*(E.^gamma)*(L.^(1-gamma));

f_p1s_c = diff(fs, kc);

f_p1s_p = diff(fs, kp);

f_p1_c = matlabFunction(f_p1s_c,'vars',{kc kp});

f_p1_p = matlabFunction(f_p1s_p,'vars',{kc kp});

u = @(c) (integral(exp(-(p-n))*c.^(1-theta)/(1-theta)));0;inf ;

% %% Steady state

% % SS for Ramsey, GR for Solow

% % Numerical solution using unidimensional Newton-Raphson

%

% metric=1; i=0; kSS=1;

% while (metric>=1e-10)

% kSS = ((delta +rho)/(A*alpha)).^(1/alpha - 1) ;

% metric = ((delta +rho)/(A*alpha)).^(1/alpha - 1); i=i+1;

% end

% cSS = f(kSS) - (delta+n)*kSS;

kSS_c = ((delta +rho)/(A*gamma)).^(1/(gamma - 1)) ;

kSS_p = ((delta +rho)/(A*gamma)).^(1/(gamma - 1)) ;

%kSS = [kSS_c kSS_p]

cSS = f(kSS_c, kSS_p) - (delta+n)*kSS_c ;

%% Grid, differentiation operators

Ik = 2000; kmin = 1e-5*kSS_c; kmax = 5*kSS_c;

k = linspace(kmin,kmax,Ik)';

dk = (kmax-kmin)/Ik;

d1Mx = spdiags(repmat([-1 sparse(1,1) 1],Ik,1),-1:1,Ik,Ik)/2;

d1Mx(1,1)=-1; d1Mx(1,2)=1; d1Mx(Ik,Ik-1)=-1; d1Mx(Ik,Ik)=1;

d1Mx = d1Mx/dk;

%% Main loop - Euler equation + Newton-Raphson (modified reflection method)

% Initialisation

metric=1; i=0;

c=k;

% Static (c-independent) component of the Jacobian

Js = spdiag(f(kc, kp) - (delta+n)*kc)*d1Mx*theta - ...

spdiag(f_p1_c(kc, kp)-delta - rho);

Unable to perform assignment because value of type 'sym' is not convertible to 'double'.

Error in spdiags>makeSparseGeneral (line 225)
V(offset) = B(i + d(k), k);

Error in spdiags (line 125)
res1 = makeSparseGeneral(B, d, A);

Error in solution>spdiag (line 112)
x = spdiags(v,0,rv,rv);

Caused by:
Error using symengine
Unable to convert expression containing symbolic variables into double array. Apply 'subs' function first to substitute values for variables.

Jd = sparse(Ik,Ik);

tic

while metric > 1e-10

% Dynamic (c-dependent) component of the Jacobian

% Jd = spdiags([-0.5*c [0; 0.5*(c(1:N-2)-c(3:N)); 0] 0.5*c],-1:1,N,N)';

% Jd(1,1) = 2*c(1)-c(2); Jd(1,2) = -c(1);

% Jd(N,N-1) = c(N); Jd(N,N) = c(N-1) - 2*c(N);

Jd = -(spdiag(d1Mx*c)+spdiag(c)*d1Mx)*theta;

% Merit function/Error vector (from the Euler equation)

err = (d1Mx*c).*(f(k) - (delta+n)*k - c)*theta - ...

(f_p1(k)-delta - rho).*c;

c = c - (Js+Jd)\err;

metric = max(abs(err));

i=i+1;

end

toc

if metric<=1e-10

fprintf('Solver converged in %d iterations.\n',i);

else

fprintf('Solver failed to converge in %d iterations!\n',i);

return;

end

s = f(k) - (delta+n)*k - c;

%% Simple sparse diagonalisation

function[x] = spdiag(v)

[rv,cv] = size(v);

if (cv==1)

x = spdiags(v,0,rv,rv);

return;

elseif (rv==1)

x = spdiags(v',0,cv,cv);

return;

else x=v; return;

end

end

0 个评论

显示 -2更早的评论隐藏 -2更早的评论

请先登录,再进行评论。

请先登录,再回答此问题。

回答(1 个)

Steven Lord 2022-8-9

  • 链接

    此回答的直接链接

    https://ww2.mathworks.cn/matlabcentral/answers/1776615-value-of-type-sym-is-not-convertible-to-double#answer_1023955

  • 链接

    此回答的直接链接

    https://ww2.mathworks.cn/matlabcentral/answers/1776615-value-of-type-sym-is-not-convertible-to-double#answer_1023955

You cannot create a sparse matrix that contains a symbolic expression. Either store the symbolic expression in a symbolic matrix or convert the symbolic expression into a numeric value and store that in the sparse matrix.

syms x

y = x.^2 % symbolic expression

y=value of type 'sym' is not convertible to 'double' (3)

S = sym(zeros(4)) % symbolic matrix

S=

value of type 'sym' is not convertible to 'double' (4)

S(2, 3) = y % Assign a symbolic expression into a symbolic matrix

S=

value of type 'sym' is not convertible to 'double' (5)

y2 = subs(y, x, 3) % numeric value

y2=

9

A = zeros(4) % numeric matrix

A = 4×4

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

A(2, 3) = y2 % Assign a numeric value into a numeric matrix

A = 4×4

0 0 0 0 0 0 9 0 0 0 0 0 0 0 0 0

You can store a numeric value in a symbolic array (which will automatically convert the numeric value into a symbolic expression) but storing a symbolic expression in a numeric array will not work unless the symbolic expression can be converted into a numeric value.

S(4, 1) = y2 % Storing a numeric value into a symbolic matrix works

S=

value of type 'sym' is not convertible to 'double' (6)

A(4, 1) = y % Storing a symbolic expression in a numeric matrix does not work

Unable to perform assignment because value of type 'sym' is not convertible to 'double'.

Caused by:
Error using symengine
Unable to convert expression containing symbolic variables into double array. Apply 'subs' function first to substitute values for variables.

1 个评论

显示 -1更早的评论隐藏 -1更早的评论

Sabrina Haumann 2022-8-9

此评论的直接链接

https://ww2.mathworks.cn/matlabcentral/answers/1776615-value-of-type-sym-is-not-convertible-to-double#comment_2308710

  • 链接

    此评论的直接链接

    https://ww2.mathworks.cn/matlabcentral/answers/1776615-value-of-type-sym-is-not-convertible-to-double#comment_2308710

Hello, thanks for your answer! But what lines do I then have to edit in my code? Sorry, I am completely new to MATLAB.

请先登录,再进行评论。

请先登录,再回答此问题。

另请参阅

类别

Mathematics and OptimizationSymbolic Math ToolboxMathematicsNumber Theory

Help CenterFile Exchange 中查找有关 Number Theory 的更多信息

标签

  • symbolic
  • double
  • code
  • matlab

产品

  • MATLAB
  • Symbolic Math Toolbox

版本

R2022a

Community Treasure Hunt

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

Start Hunting!

发生错误

由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。


Translated by value of type 'sym' is not convertible to 'double' (8)

value of type 'sym' is not convertible to 'double' (9)

选择网站

选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:

您也可以从以下列表中选择网站:

美洲

欧洲

亚太

联系您当地的办事处

value of type 'sym' is not convertible to 'double' (2024)

References

Top Articles
Fit 4 Life Murrayville Reviews
Anybody live in North Carolina? | Southeast
Section 4Rs Dodger Stadium
Patreon, reimagined — a better future for creators and fans
Restaurer Triple Vitrage
Chatiw.ib
Toyota Campers For Sale Craigslist
Mileage To Walmart
Do you need a masters to work in private equity?
Songkick Detroit
Imbigswoo
Does Pappadeaux Pay Weekly
Globe Position Fault Litter Robot
Housework 2 Jab
Rainfall Map Oklahoma
Wilmot Science Training Program for Deaf High School Students Expands Across the U.S.
Straight Talk Phones With 7 Inch Screen
Best Uf Sororities
Florida History: Jacksonville's role in the silent film industry
The Exorcist: Believer (2023) Showtimes
Buy Swap Sell Dirt Late Model
Lowe's Garden Fence Roll
Rufus Benton "Bent" Moulds Jr. Obituary 2024 - Webb & Stephens Funeral Homes
Keci News
Academy Sports Meridian Ms
Wkow Weather Radar
Del Amo Fashion Center Map
Prep Spotlight Tv Mn
6892697335
Kitchen Exhaust Cleaning Companies Clearwater
Culver's.comsummerofsmiles
Ticket To Paradise Showtimes Near Cinemark Mall Del Norte
Puffin Asmr Leak
R/Sandiego
Chadrad Swap Shop
Tamil Play.com
Http://N14.Ultipro.com
Solemn Behavior Antonym
The disadvantages of patient portals
Craigslist Odessa Midland Texas
Hovia reveals top 4 feel-good wallpaper trends for 2024
Exam With A Social Studies Section Crossword
Bustednewspaper.com Rockbridge County Va
Watch Chainsaw Man English Sub/Dub online Free on HiAnime.to
St Anthony Hospital Crown Point Visiting Hours
Sams Gas Price San Angelo
F9 2385
Great Clips Virginia Center Commons
2487872771
Amourdelavie
Myhrkohls.con
Dcuo Wiki
Latest Posts
Article information

Author: Dean Jakubowski Ret

Last Updated:

Views: 5605

Rating: 5 / 5 (70 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Dean Jakubowski Ret

Birthday: 1996-05-10

Address: Apt. 425 4346 Santiago Islands, Shariside, AK 38830-1874

Phone: +96313309894162

Job: Legacy Sales Designer

Hobby: Baseball, Wood carving, Candle making, Jigsaw puzzles, Lacemaking, Parkour, Drawing

Introduction: My name is Dean Jakubowski Ret, I am a enthusiastic, friendly, homely, handsome, zealous, brainy, elegant person who loves writing and wants to share my knowledge and understanding with you.