what is different between the codes?
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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()
driver2()
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
0 Commenti
Risposta accettata
Torsten
il 17 Dic 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 Commenti
Torsten
il 17 Dic 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()
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
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Equation Solving in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


