how can i make the initial value variable? this is the first problem. in my code, i used 'inline' to assign values to variable (t). However, the equation which is in the inline function was copied manualy. is it possible to do it automatically?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
hamit Kenan
le 28 Mai 2015
Réponse apportée : Star Strider
le 28 Mai 2015
clear all
clc
syms t y
y=dsolve('Dy-y=t^2','y(0)=1');
simplify(y)
y1=inline('3*exp(t) - 2*t - t^2 - 2','t')
t=-4;
sayici=0;
for i=1:7
t=t+1;
sayici=sayici+1;
b(sayici)=y1(t);
end
b
ezplot(y1,[-3 3])
xlabel('t [-3 3]');
ylabel('y');
grid on
2 commentaires
Réponse acceptée
Star Strider
le 28 Mai 2015
To make it a variable, declare it as such in your dsolve statement, then to use it as a variable, use matlabFunction and the plot function:
syms t y
y=dsolve('Dy-y=t^2','y(0)=y0');
simplify(y)
fy = matlabFunction(y) % Create Anonymous Function From ‘y’
t = linspace(-3, 3, 35); % Vector: ‘t’
y0 = 0:5; % Vector: ‘y0’
[T,Y0] = ndgrid(t,y0); % Create Grid Matrices
Y = fy(T,Y0); % Evaluate Function
figure(1)
plot(t, Y)
grid
legstr = strsplit(sprintf('y_0 = %.0f\n', y0), '\n');
legend(legstr(1:end-1), 'Location','NW')
The matlabFunction result is:
fy = @(t,y0)t.*-2.0+exp(t).*(y0+2.0)-t.^2-2.0
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Function Creation dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!