Im new at matlab

1 Ansicht (letzte 30 Tage)
Carlos Julian
Carlos Julian am 9 Apr. 2024
Beantwortet: Arnav am 20 Sep. 2024
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 Kommentare
Carlos Julian
Carlos Julian am 9 Apr. 2024
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 am 9 Apr. 2024
Bearbeitet: Torsten am 9 Apr. 2024
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

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Arnav
Arnav am 20 Sep. 2024
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:

Kategorien

Mehr zu Numerical Integration and Differential Equations finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by