Im new at matlab

조회 수: 1 (최근 30일)
Carlos Julian
Carlos Julian 2024년 4월 9일
답변: Arnav 2024년 9월 20일
Hello, im new at matlab and I´d like to solve this ED x2y′′−xy′+y=xln3x, when I put the code i get an error that says "Error in (line 3)
sol = dsolve(eqn)", and no solution is shown in the screen. Also, how do I graphic the family curves of the solutions indicating the parameters I used?
My code is :
syms x y(x)
eqn = x^2*diff(y,x,2) - x*diff(y,x) + y == x*log(3*x);
sol = dsolve(eqn);
disp(sol);
  댓글 수: 3
Carlos Julian
Carlos Julian 2024년 4월 9일
Dont know why I got that error, I was trying the online app and it seems that it was struggling with 3rd line, I moved to the software and got the same answer than yours. Thanxs anyway.
Do you have any idea about the second part of my question? Greetings.
Torsten
Torsten 2024년 4월 9일
편집: Torsten 2024년 4월 9일
You have to specify two conditions on y and/or dy in order to fix a solution.
I don't know what you mean by "how do I graphic the family curves of the solutions indicating the parameters I used? " As far as I can see, you don't use parameters in your differential equation.
syms x y(x)
dy = diff(y,x);
d2y = diff(dy,x);
eqn = x^2*d2y - x*dy + y == x*log(3*x);
conds = [y(1)==1,dy(1)==0];
sol = dsolve(eqn,conds);
fplot(sol,[1 5])
grid on

댓글을 달려면 로그인하십시오.

답변 (1개)

Arnav
Arnav 2024년 9월 20일
I understand that you have solved the given differential equation symbolically and are trying to plot the family of curves represented by the solution.
This can be achieved by substituting different values of C1 and C2 using subs function and plotting them using the plot function. The related code is provided below:
syms x C2 C1
C1_vals = [1, 2, 3]; % Fixed values for C1
C2_vals = [-1, 0, 1]; % Values for C2
x_vals = linspace(0.1, 10, 100); % Range for x
sol = C2*x - (x*log(x)^2*(log(27) + 2*log(x)))/6 + (x*log(3*x)^2*log(x))/2 + C1*x*log(x);
for C1_value = C1_vals
figure; % Create a new figure for each value of C1
hold on; % Hold on to plot multiple lines in the same figure
for C2_value = C2_vals
y = subs(sol,[C1 C2], [C1_value C2_value]);
y_vals = double(subs(y, x, x_vals)); % Evaluate for x
plot(x_vals, y_vals, 'DisplayName', sprintf('C2=%.1f', C2_value));
end
hold off; % Release hold after plotting
xlabel('x'); ylabel('y');
title(sprintf('Curves for C1=%.1f', C1_value)); legend('show');
end
You may refer to the following documentation links for more information about the functions subs and plot:

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differential Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by