Error : The following error occurred converting from sym to double
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Image Analyst
am 22 Sep. 2021
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?
Antworten (1)
Himanshu
am 25 Apr. 2024
Bearbeitet: Himanshu
am 25 Apr. 2024
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 Kommentare
Siehe auch
Kategorien
Mehr zu Assumptions finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!