what is different between the codes?
显示 更早的评论
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 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Equation Solving 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


