what is different between the codes?

4 views (last 30 days)
Hello, I have matlab code that calls the function "homework7" below, and code that calls the function "Examprac" below that. For the top code, it successfully returns a value for variables "M" and "K", but for the bottom code "M" gets returned as an entire equation. The codes are almost identical but for some reason the "solve" function is not working for me. Sorry for the long mass of code, but the real and only problem I am having is with the "C = solve(g, [M, K])" term.
First code:
driver1()
C = struct with fields:
M: 1/2 K: 0
C = 1×2
0.5000 0
yp = 
yp = 
y = 
driver2()
g = 
C = struct with fields:
M: -(40000000*cos((117*x)/10) - 56000000*sin((117*x)/10))/(1254221280*cos((117*x)/10) + 27288860249597*sin((117*x)/10)) K: 0
Error using symengine
Unable to convert expression containing symbolic variables into double array. Apply 'subs' function first to substitute values for variables.

Error in sym/double (line 761)
Xstr = mupadmex('symobj::double', S.s, 0);

Error in solution>Examprac (line 55)
M_value = double(C.M);

Error in solution>driver2 (line 44)
y_new = Examprac(x0,y0,coeff,rx);
function [] = driver1()
syms x
x0 = [0,0,0,0];
y0 = [1,2,-1,-32];
coeff = [1,0,5,0,4];
rx = 90*sin(4*x);
y = homework7(x0,y0,coeff,rx)
end
Function called:
function y = homework7(x0,y0,coeff,rx)
syms x c1 c2 c3 c4 K M
R = roots(coeff);
R = sqrt(R);
R_neg = R*(-1);
a = zeros(size(R));
b = zeros(size(R));
for i = 1:length(R)
a(i) = imag(R(i));
b(i) = imag(R_neg(i));
end
yh = c1*cos(a(2)*x) + c2*sin(a(2)*x) + c3*cos(a(1)*x) + c4*sin(a(1)*x);
yp = K*cos(4*x) + M*sin(4*x);
yp_der1 = diff(yp);
yp_der2 = diff(yp_der1);
yp_der3 = diff(yp_der2);
yp_der4 = diff(yp_der3);
g = yp_der4 + 5*yp_der2 + 4*yp == rx;
C = solve(g, [M, K])
M_value = double(C.M);
K_value = double(C.K);
C = [M_value, K_value]
yp = subs(yp,K,C(2))
yp = subs(yp,M,C(1))
y = yh + yp;
end
Second Code:
function [] = driver2()
syms x K M c1 c2 c3 c4
x0 = [0,0,0,0];
y0 = [1,200,-80,900];
coeff = [1,-0.02,12.5,-0.058,27.77];
rx = -0.05*exp(0.005*x)*cos(11.7*x) + 0.07*exp(0.005*x)*sin(11.7*x);
y_new = Examprac(x0,y0,coeff,rx);
end
Function called:
function y = Examprac(x0,y0,coeff,rx)
syms x c1 c2 c3 c4 K M
yp = 2*K*cos((117*x)/10)*exp(x/200) + 2*M*sin((117*x)/10)*exp(x/200);
yp_der1 = diff(yp);
yp_der2 = diff(yp_der1);
yp_der3 = diff(yp_der2);
yp_der4 = diff(yp_der3);
g = coeff(1)*yp_der4 + coeff(2)*yp_der3 + coeff(3)*yp_der2 + coeff(4)*yp_der1 + coeff(5)*yp == rx
C = solve(g, [M, K])
M_value = double(C.M);
K_value = double(C.K);
C = [M_value, K_value]
yp = subs(yp,K,C(2))
yp = subs(yp,M,C(1))
y = yp;
end

Accepted Answer

Torsten
Torsten on 17 Dec 2023
In both cases, you want to solve one equation in three symbolic variables (x,K,M).
In your first code, MATLAB is able to determine K and M as numerical values (0 and 0.5). In this case, you can apply "double" on the solution to get numerical values back.
In your second code, MATLAB is only able to determine M as a function of x. A function cannot be converted to a double - that's why the error appears.
  2 Comments
Sean Rotmansky
Sean Rotmansky on 17 Dec 2023
Hello!
Yes, I can see that is the issue, is there a reason it can't determine a numerical value for M? When I solve it by hand (and in the solution to the quiz) both M and K should come out to zero.
Torsten
Torsten on 17 Dec 2023
When I solve it by hand (and in the solution to the quiz) both M and K should come out to zero.
This doesn't seem to be a solution:
driver2()
g = 
ans = 
function [] = driver2()
syms x K M c1 c2 c3 c4
x0 = [0,0,0,0];
y0 = [1,200,-80,900];
coeff = [1,-0.02,12.5,-0.058,27.77];
rx = -0.05*exp(0.005*x)*cos(11.7*x) + 0.07*exp(0.005*x)*sin(11.7*x);
y_new = Examprac(x0,y0,coeff,rx);
end
function y = Examprac(x0,y0,coeff,rx)
syms x c1 c2 c3 c4 K M
yp = 2*K*cos((117*x)/10)*exp(x/200) + 2*M*sin((117*x)/10)*exp(x/200);
yp_der1 = diff(yp);
yp_der2 = diff(yp_der1);
yp_der3 = diff(yp_der2);
yp_der4 = diff(yp_der3);
g = coeff(1)*yp_der4 + coeff(2)*yp_der3 + coeff(3)*yp_der2 + coeff(4)*yp_der1 + coeff(5)*yp == rx
subs(g,[M,K],[0 0])
y = 1;
end

Sign in to comment.

More Answers (0)

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!