I have an error while plotting

2 ビュー (過去 30 日間)
Thomas van de Wiel
Thomas van de Wiel 2015 年 12 月 31 日
回答済み: Star Strider 2015 年 12 月 31 日
I have the following code (It's a differential equation)
function conv = h4_5(t,q)
a=8/3;
b=28;
c=10;
x=q(1);
y=q(2);
z=q(3);
conv=zeros(3,1);
conv(1)=c*(y-x);
conv(2)=x*(b-z)-y;
conv(3)=x*y-a*z;
end
but when I'm trying to solve and plot it with
[t,q]=ode45('h4_5',[0 200],[0 1 0]);
plot3(q(1),q(2),q(3),'b')
view(-10,10)
xlabel('x')
ylabel('y')
zlabel('z')
all I get is an empty graph. No errors or something, just an empty graph. The goal of this exercise is plotting convection in the atmosphere with aid of the three given differential equations (displayed in first part of code behind conv(1), 2 and 3. I hope someone knows how to fix this.

採用された回答

Star Strider
Star Strider 2015 年 12 月 31 日
You need to call your ‘h4_5’ equation correctly using function handle syntax (adding the ‘@’ sign) in your ode45 call. Then recognising that you want to plot the columns of the ‘q’ matrix ode45 returns, you have to address them as such.
With these minor changes, this works:
[t,q]=ode45(@h4_5,[0 200],[0 1 0]);
plot3(q(:,1),q(:,2),q(:,3),'b')
view(-10,10)
xlabel('x')
ylabel('y')
zlabel('z')
grid on
I added the grid call.
Also, note that conv is a MATLAB bulit-in function for convolution. It doesn’t make any difference here because you’re not using it otherwise in your ODE function and a function workspace is unique to it, but it’s best to avoid using any function name as a variable name. This is called ‘overshadowing’ and can cause serious problems in your code, because MATLAB will use the variable definition rather than the function definition if you have already used a specific name as a variable.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by