I do not know how to fix this problem (code below)
조회 수: 1 (최근 30일)
이전 댓글 표시
%ode45_ex.m
%This program solves a system of 3 differential equations
%by using ode45 function
%y1'=y2*y3*t, y2'=-y1*y3, y3'=-0.51*y1*y2
%y1(0)=0, y2(0)=1.0, y3(0)=1.0
clear;
clc;
initial=[0.0 1.0 1.0]
tspan=0.0:0.1:10.0;
options=odeset('RelTol', 1.0e-6, 'AbsTol', [1.0e-6 1.0e-6 1.0e-6]);
[t, Y]=ode45(@dydt3, tspan, initial, options);
P=[t Y];
disp(P);
t1=P(:,1);
y1=P(:,2);
y2=P(:,3);
y3=P(:,4);
fid=fopen('output.txt','w');
fprintf(fid,' t y(1) y(2) y(3) \n');
fprintf(fid, '------------------------------\n');
for i=1:2:101
fprintf(fid, ' %6.2f %10.2f %10.2f %10.2 \n' t1(i), y1(i), y2(i), y3(i)); %HERE
end
fclose(fid);
plot(t1, Y(:,1), t1, Y(:,2), '-.', t1, Y(:,3), '--'),
xlabel('t'), ylabel('Y(1), Y(2), Y(3)')
title('Y vs. t');
grid
text(6.0, -1.2, 'y(1)')
text(7.7, -0.25, 'y(2)')
text(4.2, 0.85, 'y(3)');
The program keeps saying that something is wrong with the t1. I bolded where it says the problem is.
Thanks for your help
댓글 수: 1
Steven Lord
2018년 6월 7일
Please copy and paste the full text of the error message you receive (all the red text) into a comment. Don't paraphrase, post it exactly as it appears in the Command Window.
채택된 답변
Aquatris
2018년 6월 7일
You are missing a comma (,) before the t1 there. You are also missing a comma at line "plot(t1,.." before the "title".
Also in the code you provide, you do not give "@dydt3" function any further mistakes cannot be found.
댓글 수: 0
추가 답변 (0개)
참고 항목
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!