Home Problems with plotting in Matlab
Reply: 1

Problems with plotting in Matlab

nisse Published in 2011-06-04 14:50:00Z

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


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


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 )


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

for k=2:2:n-2

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


for k = 1:2:n-1

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


I = I * h/3;

tid = toc;


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?

Bryan Reply to 2011-06-04 15:10:17Z

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.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.333317 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO