Plot polynomial using Eueler's method

syms x;
y=x.^3 + x.^2- 12*x; %polynomial function
dy= diff(y,x)%derivative of y
N=100;
h=0.1;
x=-4:0.1:3;
y(1)=-10;
y=zeros(size(x));
EM=[]; %empty array to store values
for n=1:N
y(n+1)=y(n)+h*dy(n);
x(n+1)=n*h;
EM(x,y)=[x,y];
end
plot(EMsub,"r-")
Hello, this is the code that i have so far. I have tried writing a code to plot f(x)=x^3+x^2-12x using Euler's method in matlab. I can seem to figure out why the code is not running. At line 12 i keep getting the following error:
Error using symengine
Unable to convert expression containing symbolic variables into double array. Apply 'subs' function first to substitute values for variables.
I am not exactly sure how to fix this error or what is wrong. Could someone explain why its not working and what i am doing wrong here? Any help would be appreciated.

 採用された回答

Davide Masiello
Davide Masiello 2022 年 10 月 8 日
編集済み: Davide Masiello 2022 年 10 月 8 日

1 投票

so, initially, you calculate the analytical derivative of the function using the symbolic environment
syms x
y = x^3+x^2-12*x; % polynomial function
yp = diff(y,x) % symbolic derivative of y
yp = 
You'll need this symbolic derivative to be turned into a matlab function, which you can to like this
dydx = matlabFunction(yp)
dydx = function_handle with value:
@(x)x.*2.0+x.^2.*3.0-1.2e+1
Now, you can solve the ODE represented by dydx using Euler's method.
h = 0.1;
xn = -5:h:5;
yn = zeros(size(xn));
yn(1) = -40;
for idx = 2:length(xn)
yn(idx) = yn(idx-1)+h*dydx(xn(idx-1));
end
Now, you can plot both numerical and analytical solutions for comparison
fplot(y)
hold on
plot(xn,yn,'k--')
legend('Analytical solution','Euler''s method')

5 件のコメント

E
E 2022 年 10 月 8 日
Thank you so much. I have a question though. Why wasn't my previous code running? What was i missing? I also noticed the changes in the for loop. From what i know Euler's method changes using step sizes and increasing (n+1) in a range of n=1:N. Why start at 2, and remove 1 instead of adding it?
Davide Masiello
Davide Masiello 2022 年 10 月 8 日
There are a number of problems with your code, I can try and summarise them below
syms x;
y=x.^3 + x.^2- 12*x;
dy= diff(y,x)
f= @(x,y)3*x.^2+2*x-12; % no need to write the derivative function yourself
N=100; % no need to specify the number of points if you specify interval h and x0 and xf
h=0.1;
x=-4:0.1:3;
y(1)=-10; % you should specify the initial condition after initializing the solution array y
y=zeros(size(x)); % you already used y as a symbolic variable, you need to use a different name for the numerical one
EM=[]; % no need for this, the values will fill up the array you initialised in the previous line.
for n=1:N %since you have n+1 below, the indexes cannot span 1:N, but either 1:N-1 or 2:N
y(n+1)=y(n)+h*dy(n);
x(n+1)=n*h; % x is defined already, no need to recompute it
EM(x,y)=[x,y]; % you can't use x and y as indexes
end
plot(EMsub,"r-")
John D'Errico
John D'Errico 2022 年 10 月 8 日
編集済み: John D'Errico 2022 年 10 月 8 日
Your code was not terrible. But you need to worry more about what variables you use, and about moving from the symbolic side of MATLAB, to the numerical side. For example,
syms x;
y=x.^3 + x.^2- 12*x; %polynomial function
dy= diff(y,x)%derivative of y
dy = 
At this point, y and dy are symbolic expressions in MATLAB.
whos y dy
Name Size Bytes Class Attributes dy 1x1 8 sym y 1x1 8 sym
But then later on, you tried to use y to store a vector of numbers, as the solution to the ODE. That might cause a problem. ;-) Be careful about re-using the same variable names. Yes, you did reassign y. But what did you do?
y(1)=-10;
So, first, you assigned the initial condition to y. But that does not actually do what you think. It overwrites the symbolic function y that you created.
y
y = 
And THEN you essentially did this:
x=-4:0.1:3;
y=zeros(size(x));
So you have now lost the information about the initial condition you tried to enforce, because you have now just overwritten the variable y.
As far as the code by Davide goes, you ask why N was started at 2. Since you already know the value of y(1), you don't need to use the Euler step to compute y(1) at all. So that only applies to the values y(2) through y(N).
But as I said, your code was not too far from actually working. I would say you mainly need to take a little more care, thinking about what your variables store, and exactly what they mean at that point in time. As well, I'd be willing to use new variable names, to avoid confusion in your code.
E
E 2022 年 10 月 8 日
編集済み: E 2022 年 10 月 9 日
Thank you so much everyone for your helpful feedback. Seeing my code not working and not knowing why was truly frustrating. Matlab is a challenge. I read your comments and i think i understand where my code was running into issues. Thank you again for your help on this.
Steven Lord
Steven Lord 2022 年 10 月 9 日
Since it sounds like you're new to MATLAB, I suggest you start with the free MATLAB Onramp tutorial to quickly learn the essentials of MATLAB.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

質問済み:

E
E
2022 年 10 月 8 日

編集済み:

E
E
2022 年 10 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by