Im new at matlab
Show older comments
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 Comments
What's the complete error message?
Your code runs without error here:
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);
Carlos Julian
on 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
Answers (1)
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:
- subs: https://www.mathworks.com/help/symbolic/sym.subs.html
- plot: https://www.mathworks.com/help/matlab/ref/plot.html
Categories
Find more on Equation Solving in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


