简体   繁体   中英

Problems with plotting in Matlab

Hey, I've got a problem plotting a function in Matlab.

I first run this:

format long f = inline('-x.^2');

for i = 0:10 [I(i+1) h(i+1) tid(i+1)] = trapets(f,0,1,2^i); end

trunk = I - log(2);

hold on grid on

plot(log(h),log(trunk),'r+')

t = -7:0; c = polyfit(log(h),log(trunk),1); yy = polyval(c,t); plot(t,yy) grid off hold off

koefficienter = real(c)

and after that I run this file:

hold on

plot(h,trunk,'r+:','linewidth',2)

axis([0 0.6 0 0.0014])

Thing is, I don't get any errors, and the plot windows pops up with axes and all, but there is no graph to be found. It's just an empty window with two axes.

Anyone got any ideas?

Edit: Okay, so I'm new to this site and couldn't find the reply button, so I add a reply here instead.

@woodchips: I just realized that I hadn't given you all the information for this problem.. Sorry about that, anyhow I would really appreciate it if someone had the time to help me with this, it would seriously make my week.

This is the part I accidentally left out:

function [ I,h,tid ] = trapets( f,a,b,n )

h=(ba)/n;

tic; I=(f(a)+f(b));

for k=2:2:n-2

 I = I+2*f(a+k*h);

end

for k = 1:2:n-1

 I = I + 4*f(a+k*h);

end

I = I * h/3;

tid = toc;

end

Edit 2: So, I think that the graph I'm seeking is actually getting plotted in the first code that I wrote, the problem is that the variabe 'I' is not changing, which I expect it to do, although the variabels 'n' and 'h' do change. If 'I' was working correctly, I would probably get the right graph (hopefully). Any ideas, anyone?

Unfortunately the home computer I had with Matlab on it died the other day so I can't test anything. First thing I can think of if to simply run step by step through the code and see if the results of the math are what you are expecting. For instance Matlab was primarily made and runs as a matrix calculator if I recall correctly. As such most of the simple math doesn't function as it would punching it in a calculator. An example would be that 2^i needs to be 2.^i to operate correctly in some cases. Same with.* and./ to use the singular scalar verses the matrices math.

The best way to find out what is going wrong is to iterate through the math a few times to ensure that it is being performed as expected. Once that is verified then you can move on to looking at plotting formatting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM