Error : The following error occurred converting from sym to double
조회 수: 2 (최근 30일)
이전 댓글 표시
So I'm running a programme in which there are two symbolic variables and the rest everything is constant, and hu(x,1) is picking up 100 values from a data set at random. When hu(1,1) > 8 is used in 1st iteartion, the code runs and returns LS1(1,1) = 0, but when hu(2,1) < 8 is used in next iteration, the code stops since it is now a symbolic number for LS1(2,1) and returns this error. I'd appreciate it if someone could assist me with this. (mu, SWm, and roww are all constant)
syms hu1 vk
if hu(x,1) > 8
if hu(x,1)> 10
BF(x,1) = ( 2*(0.5*(0.5+0.2)*1.2)*L);
Fd(x,1) = 0.5*Cd(x,1)*(vk^2).*L*s;
else if hu(x,1) < 10
BF(x,1) = (2*(0.5*(0.5+0.2)*(hu1- 3.1))*L);
Fd(x,1) = 0.5*Cd(x,1)*roww*(vk^2).*L*(hu1-hb);
end
end
LS1(x,1) = (Fd(x,1) - (mu*(SWm - BF(x,1))));
else
LS1(x,1) = 0;
Error : The following error occurred converting from sym to double (for LS1(x,1)
This error is popping out in second iteration. How to deal with this?
댓글 수: 2
Image Analyst
2021년 9월 22일
Do they need to be symbolic? What if you delete the line
syms hu1 vk
and just assign them (if needed) in advance of your other code where you use them?
답변 (1개)
Himanshu
2024년 4월 25일
편집: Himanshu
2024년 4월 25일
Hey,
The issue in your code arises from the fact that you're trying to assign symbolic expressions to elements of LS1, which is initially defined as a symbolic array. MATLAB is unable to convert symbolic expressions to double-precision numbers when you attempt to perform arithmetic operations involving both symbolic and numeric variables.
Here are the issues with your current code:
- You're working with symbolic variables hu1 and vk. In your code, you have conditions based on the symbolic variable hu(x,1).
- When hu(x,1) > 8, you perform calculations involving symbolic variables and numeric constants, which is fine.
- However, when hu(x,1) < 8 in the second iteration, the code tries to assign symbolic expressions to elements of LS1, which causes MATLAB to throw an error because it cannot convert these symbolic expressions to double-precision numbers.
To address this issue, you need to ensure that LS1 is defined as a numeric array before assigning values to its elements. You can achieve this by initializing LS1 as a numeric array before the loop or before the conditions that involve symbolic calculations.
Hope this helps!
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Assumptions에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!