ํ•„ํ„ฐ ์ง€์šฐ๊ธฐ
ํ•„ํ„ฐ ์ง€์šฐ๊ธฐ

trouble coding for ODE's

์กฐํšŒ ์ˆ˜: 3 (์ตœ๊ทผ 30์ผ)
C
C 2021๋…„ 12์›” 17์ผ
๋‹ต๋ณ€: Star Strider 2021๋…„ 12์›” 17์ผ
Given ๐‘š๐‘ฅฬˆ+ ๐‘๐‘ฅฬ‡ + ๐‘˜๐‘ฅ = ๐‘ ๐‘–๐‘›(๐œ”๐‘ก) I need to write a code that prompts the user to input values for m, c, k and ๐œ”, then to find the general solution of the ODE. Then it needs to find the exact solution of the ODE using time array between 0s and 10s with a step of 0.1 s, given that ๐‘ฅ(0) = 0 and ๐‘ฅฬ‡(0) = 1.
  ๋Œ“๊ธ€ ์ˆ˜: 2
Mitchell Thurston
Mitchell Thurston 2021๋…„ 12์›” 17์ผ
do you have anything so far? because it sounds like you're just asking someone to do your homework
C
C 2021๋…„ 12์›” 17์ผ
syms x(t) m c k w % these are the terms in the ode
ode=m.*diff(diff(x(t),t))+c.*diff(x(t),t)+k.*x(t)== sin(w.*t); % this is the given ode
gen_sol=dsolve(ode) % this provides the general solution
M=input('Enter a value for the mass: ');
C=input('Enter a value for the damping coefficient: ');
K=input('Enter a value for the spring constant: ');
W=input('Enter a value for the frequency: ');
new_soln=subs(gen_sol,{'m' 'c' 'k' 'w'},{M,C,K,W}) % subs input values for variables
t=0:.1:10
exact_soln=dsolve(new_soln, 'x(0)=0','diff(x(0))=1')

๋Œ“๊ธ€์„ ๋‹ฌ๋ ค๋ฉด ๋กœ๊ทธ์ธํ•˜์‹ญ์‹œ์˜ค.

๋‹ต๋ณ€ (2๊ฐœ)

Mitchell Thurston
Mitchell Thurston 2021๋…„ 12์›” 17์ผ
this is definintely an inefficient way of doing it, but this accomplishes everything
syms dx(t) x(t) m c k w % these are the terms in the ode
dx = diff(x);
ode=m.*diff(diff(x(t),t))+c.*diff(x(t),t)+k.*x(t)== sin(w.*t); % this is the given ode
M=input('Enter a value for the mass: ');
C=input('Enter a value for the damping coefficient: ');
K=input('Enter a value for the spring constant: ');
W=input('Enter a value for the frequency: ');
new_eqn=subs(ode,{'m' 'c' 'k' 'w'},{M C K W})
new_sol=dsolve(new_eqn, x(0)==0, dx(0)==1)
fplot(new_sol, [0,10])
% to get the exact value at the timestep
t = 0:.1:10;
xt = zeros(size(t));
for i =1:length(t)
xt(i) = sym2poly(subs(new_sol,'t',t(i)));
end

Star Strider
Star Strider 2021๋…„ 12์›” 17์ผ
The code works as far as it goes. The code needs to include initial conditions for and as separate parameters (call them and or something else appropriate), include them in the dsolve call as arguments, and use the simplify function to simplify the resulting expression, with 'steps',500 to be certain it simplifies as much as possible. Then use the matlabFunction function (use the name-value pair 'Vars' argument, and provide it appropriate associated values) to produce an anonymous function that can be executed numerically. Then, supply the appropriate arguments to the anonymous function and the appropriate time vector to complete the assignment.
.

์นดํ…Œ๊ณ ๋ฆฌ

Help Center ๋ฐ File Exchange์—์„œ Ordinary Differential Equations์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ธฐ

Community Treasure Hunt

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

Start Hunting!

Translated by